xarray.DataArray.rolling¶
-
DataArray.
rolling
(min_periods=None, center=False, **windows)¶ Rolling window object.
Rolling window aggregations are much faster when bottleneck is installed.
Parameters: min_periods : int, 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 : boolean, default False
Set the labels at the center of the window.
**windows : dim=window
- dim : str
Name of the dimension to create the rolling iterator along (e.g., time).
- window : int
Size of the moving window.
Returns: rolling : type of input argument
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('15/12/1999', ... periods=12, freq=pd.DateOffset(months=1))], ... dims='time') >>> da <xarray.DataArray (time: 12)> array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.]) Coordinates: * time (time) datetime64[ns] 1999-12-15 2000-01-15 2000-02-15 ... >>> da.rolling(time=3).mean() <xarray.DataArray (time: 12)> array([ nan, nan, 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]) Coordinates: * time (time) datetime64[ns] 1999-12-15 2000-01-15 2000-02-15 ...
Remove the NaNs using
dropna()
:>>> da.rolling(time=3).mean().dropna('time') <xarray.DataArray (time: 10)> array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]) Coordinates: * time (time) datetime64[ns] 2000-02-15 2000-03-15 2000-04-15 ...