Attention
This repository has been archived. Please use xarray.DataTree instead.
datatree.DataTree.bfill#
- DataTree.bfill(dim: Hashable, limit: int | None = None) Self [source]#
Fill NaN values by propagating values backward
Requires bottleneck.
- Parameters:
dim (
Hashable
) – Specifies the dimension along which to propagate values when filling.limit (
int
orNone
, optional) – 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).
Examples
>>> time = pd.date_range("2023-01-01", periods=10, freq="D") >>> data = np.array( ... [1, np.nan, np.nan, np.nan, 5, np.nan, np.nan, 8, np.nan, 10] ... ) >>> dataset = xr.Dataset({"data": (("time",), data)}, coords={"time": time}) >>> dataset <xarray.Dataset> Size: 160B Dimensions: (time: 10) Coordinates: * time (time) datetime64[ns] 80B 2023-01-01 2023-01-02 ... 2023-01-10 Data variables: data (time) float64 80B 1.0 nan nan nan 5.0 nan nan 8.0 nan 10.0
# filled dataset, fills NaN values by propagating values backward
>>> dataset.bfill(dim="time") <xarray.Dataset> Size: 160B Dimensions: (time: 10) Coordinates: * time (time) datetime64[ns] 80B 2023-01-01 2023-01-02 ... 2023-01-10 Data variables: data (time) float64 80B 1.0 5.0 5.0 5.0 5.0 8.0 8.0 8.0 10.0 10.0
# Limit the backward filling to a maximum of 2 consecutive NaN values
>>> dataset.bfill(dim="time", limit=2) <xarray.Dataset> Size: 160B Dimensions: (time: 10) Coordinates: * time (time) datetime64[ns] 80B 2023-01-01 2023-01-02 ... 2023-01-10 Data variables: data (time) float64 80B 1.0 nan 5.0 5.0 5.0 8.0 8.0 8.0 10.0 10.0
- Returns:
See also
Dataset.ffill