ffill

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

Fill NaN values by propagating values forward

Requires bottleneck.

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

  • limit (int or None, default: None) – The maximum number of consecutive NaN values to forward 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(
...     [
...         [np.nan, 1, 3],
...         [0, np.nan, 5],
...         [5, np.nan, np.nan],
...         [3, np.nan, np.nan],
...         [0, 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([[nan,  1.,  3.],
       [ 0., nan,  5.],
       [ 5., nan, nan],
       [ 3., nan, nan],
       [ 0.,  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.ffill(dim="Y", limit=None)
<xarray.DataArray (Y: 5, X: 3)> Size: 120B
array([[nan,  1.,  3.],
       [ 0.,  1.,  5.],
       [ 5.,  1.,  5.],
       [ 3.,  1.,  5.],
       [ 0.,  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.ffill(dim="Y", limit=1)
<xarray.DataArray (Y: 5, X: 3)> Size: 120B
array([[nan,  1.,  3.],
       [ 0.,  1.,  5.],
       [ 5., nan,  5.],
       [ 3., nan, nan],
       [ 0.,  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