Skip to content

Commit 848a5fd

Browse files
committed
xarray.dataset.ffill
1 parent 760aa4a commit 848a5fd

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

xarray/core/dataset.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6025,6 +6025,34 @@ def ffill(self: T_Dataset, dim: Hashable, limit: int | None = None) -> T_Dataset
60256025
than 0 or None for no limit. Must be None or greater than or equal
60266026
to axis length if filling along chunked axes (dimensions).
60276027
6028+
Example
6029+
-------
6030+
# Create a sample dataset with missing values
6031+
>>> time = pd.date_range("2023-01-01", periods=10, freq="D")
6032+
>>> data = np.array([1, np.nan, 3, np.nan, 5, 6, np.nan, 8, np.nan, 10])
6033+
>>> dataset = xr.Dataset({"data": (("time",), data)}, coords={"time": time})
6034+
6035+
# Perform forward fill (ffill) on the dataset
6036+
>>> filled_dataset = dataset.ffill(dim="time")
6037+
6038+
# Print the original dataset
6039+
>>> dataset
6040+
<xarray.Dataset>
6041+
Dimensions: (time: 10)
6042+
Coordinates:
6043+
* time (time) datetime64[ns] 2023-01-01 2023-01-02 ... 2023-01-10
6044+
Data variables:
6045+
data (time) float64 1.0 nan 3.0 nan 5.0 6.0 nan 8.0 nan 10.0
6046+
6047+
# Print the filled dataset, fills NaN values by propagating values forward
6048+
>>> filled_dataset
6049+
<xarray.Dataset>
6050+
Dimensions: (time: 10)
6051+
Coordinates:
6052+
* time (time) datetime64[ns] 2023-01-01 2023-01-02 ... 2023-01-10
6053+
Data variables:
6054+
data (time) float64 1.0 1.0 3.0 3.0 5.0 6.0 6.0 8.0 8.0 10.0
6055+
60286056
Returns
60296057
-------
60306058
Dataset

0 commit comments

Comments
 (0)