xarray.DataArray.rolling

xarray.DataArray.rolling#

DataArray.rolling(dim=None, min_periods=None, center=False, **window_kwargs)[source]#

Rolling window object for DataArrays.

Parameters:
  • dim (dict, optional) – Mapping from the dimension name to create the rolling iterator along (e.g. time) to its moving window size.

  • min_periods (int or None, default: None) – Minimum number of observations in window required to have a value (otherwise result is NA). The default, None, is equivalent to setting min_periods equal to the size of the window.

  • center (bool or Mapping to int, default: False) – Set the labels at the center of the window. The default, False, sets the labels at the right edge of the window.

  • **window_kwargs (optional) – The keyword arguments form of dim. One of dim or window_kwargs must be provided.

Returns:

core.rolling.DataArrayRolling

Examples

Create rolling seasonal average of monthly data e.g. DJF, JFM, …, SON:

>>> da = xr.DataArray(
...     np.linspace(0, 11, num=12),
...     coords=[
...         pd.date_range(
...             "1999-12-15",
...             periods=12,
...             freq=pd.DateOffset(months=1),
...         )
...     ],
...     dims="time",
... )
>>> da
<xarray.DataArray (time: 12)> Size: 96B
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11.])
Coordinates:
  * time     (time) datetime64[ns] 96B 1999-12-15 2000-01-15 ... 2000-11-15
>>> da.rolling(time=3, center=True).mean()
<xarray.DataArray (time: 12)> Size: 96B
array([nan,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., nan])
Coordinates:
  * time     (time) datetime64[ns] 96B 1999-12-15 2000-01-15 ... 2000-11-15

Remove the NaNs using dropna():

>>> da.rolling(time=3, center=True).mean().dropna("time")
<xarray.DataArray (time: 10)> Size: 80B
array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
Coordinates:
  * time     (time) datetime64[ns] 80B 2000-01-15 2000-02-15 ... 2000-10-15