bfill

UnitsAwareDataArray.bfill(dim: Hashable, limit: int | None = None) Self

Fill NaN values by propagating values backward

Requires bottleneck.

Parameters:
  • dim (str) – Specifies the dimension along which to propagate values when filling.

  • limit (int or None, default: None) – The maximum number of consecutive NaN values to backward fill. In other words, if there is a gap with more than this number of consecutive NaNs, it will only be partially filled. Must be greater than 0 or None for no limit. Must be None or greater than or equal to axis length if filling along chunked axes (dimensions).

Returns:

filled

Return type:

DataArray

Examples

>>> temperature = np.array(
...     [
...         [0, 1, 3],
...         [0, np.nan, 5],
...         [5, np.nan, np.nan],
...         [3, np.nan, np.nan],
...         [np.nan, 2, 0],
...     ]
... )
>>> da = xr.DataArray(
...     data=temperature,
...     dims=["Y", "X"],
...     coords=dict(
...         lat=("Y", np.array([-20.0, -20.25, -20.50, -20.75, -21.0])),
...         lon=("X", np.array([10.0, 10.25, 10.5])),
...     ),
... )
>>> da
<xarray.DataArray (Y: 5, X: 3)> Size: 120B
array([[ 0.,  1.,  3.],
       [ 0., nan,  5.],
       [ 5., nan, nan],
       [ 3., nan, nan],
       [nan,  2.,  0.]])
Coordinates:
    lat      (Y) float64 40B -20.0 -20.25 -20.5 -20.75 -21.0
    lon      (X) float64 24B 10.0 10.25 10.5
Dimensions without coordinates: Y, X

Fill all NaN values:

>>> da.bfill(dim="Y", limit=None)
<xarray.DataArray (Y: 5, X: 3)> Size: 120B
array([[ 0.,  1.,  3.],
       [ 0.,  2.,  5.],
       [ 5.,  2.,  0.],
       [ 3.,  2.,  0.],
       [nan,  2.,  0.]])
Coordinates:
    lat      (Y) float64 40B -20.0 -20.25 -20.5 -20.75 -21.0
    lon      (X) float64 24B 10.0 10.25 10.5
Dimensions without coordinates: Y, X

Fill only the first of consecutive NaN values:

>>> da.bfill(dim="Y", limit=1)
<xarray.DataArray (Y: 5, X: 3)> Size: 120B
array([[ 0.,  1.,  3.],
       [ 0., nan,  5.],
       [ 5., nan, nan],
       [ 3.,  2.,  0.],
       [nan,  2.,  0.]])
Coordinates:
    lat      (Y) float64 40B -20.0 -20.25 -20.5 -20.75 -21.0
    lon      (X) float64 24B 10.0 10.25 10.5
Dimensions without coordinates: Y, X