xarray.Dataset.coarsen¶
-
Dataset.
coarsen
(self, dim: Mapping[Hashable, int] = None, boundary: str = 'exact', side: Union[str, Mapping[Hashable, str]] = 'left', coord_func: str = 'mean', keep_attrs: bool = None, **window_kwargs: int)¶ Coarsen object.
- Parameters
dim (dict, optional) –
Mapping from the dimension name to the window size.
- dimstr
Name of the dimension to create the rolling iterator along (e.g., time).
- windowint
Size of the moving window.
boundary ('exact' | 'trim' | 'pad') – If ‘exact’, a ValueError will be raised if dimension size is not a multiple of the window size. If ‘trim’, the excess entries are dropped. If ‘pad’, NA will be padded.
side ('left' or 'right' or mapping from dimension to 'left' or 'right') –
coord_func (function (name) that is applied to the coordinates,) – or a mapping from coordinate name to function (name).
keep_attrs (bool, optional) – If True, the object’s attributes (attrs) will be copied from the original object to the new one. If False (default), the new object will be returned without attributes.
- Returns
Coarsen object (core.rolling.DataArrayCoarsen for DataArray,
core.rolling.DatasetCoarsen for Dataset.)
Examples
Coarsen the long time series by averaging over every four days.
>>> da = xr.DataArray( ... np.linspace(0, 364, num=364), ... dims="time", ... coords={"time": pd.date_range("15/12/1999", periods=364)}, ... ) >>> da <xarray.DataArray (time: 364)> array([ 0. , 1.002755, 2.00551 , ..., 361.99449 , 362.997245, 364. ]) Coordinates: * time (time) datetime64[ns] 1999-12-15 1999-12-16 ... 2000-12-12 >>> >>> da.coarsen(time=3, boundary="trim").mean() <xarray.DataArray (time: 121)> array([ 1.002755, 4.011019, 7.019284, ..., 358.986226, 361.99449 ]) Coordinates: * time (time) datetime64[ns] 1999-12-16 1999-12-19 ... 2000-12-10 >>>