Skip to content

Add examples for DataArrayRolling.reduce() #2968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions doc/computation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,35 @@ name of the dimension as a key (e.g. ``y``) and the window size as the value

arr.rolling(y=3)

The label position and minimum number of periods in the rolling window are
controlled by the ``center`` and ``min_periods`` arguments:
Aggregation and summary methods can be applied directly to the ``Rolling``
object:

.. ipython:: python

arr.rolling(y=3, min_periods=2, center=True)
r = arr.rolling(y=3)
r.reduce(np.std)
r.mean()

Aggregation and summary methods can be applied directly to the ``Rolling`` object:
Aggregation results are assigned the coordinate at the end of each window by
default, but can be centered by passing ``center=True`` when constructing the
``Rolling`` object:

.. ipython:: python

r = arr.rolling(y=3)
r = arr.rolling(y=3, center=True)
r.mean()

As can be seen above, aggregations of windows which overlap the border of the
array produce ``nan``s. Setting ``min_periods`` in the call to ``rolling``
changes the minimum number of observations within the window required to have
a value when aggregating:

.. ipython:: python

r = arr.rolling(y=3, min_periods=2)
r.mean()
r = arr.rolling(y=3, center=True, min_periods=2)
r.mean()
r.reduce(np.std)

Note that rolling window aggregations are faster when bottleneck_ is installed.

Expand Down
23 changes: 23 additions & 0 deletions xarray/core/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,29 @@ def reduce(self, func, **kwargs):
-------
reduced : DataArray
Array with summarized data.

Examples
--------
>>> da = DataArray(np.arange(8).reshape(2, 4), dims=('a', 'b'))
>>>
>>> rolling = da.rolling(b=3)
>>> rolling.construct('window_dim')
<xarray.DataArray (a: 2, b: 4, window_dim: 3)>
array([[[np.nan, np.nan, 0], [np.nan, 0, 1], [0, 1, 2], [1, 2, 3]],
[[np.nan, np.nan, 4], [np.nan, 4, 5], [4, 5, 6], [5, 6, 7]]])
Dimensions without coordinates: a, b, window_dim
>>>
>>> rolling.reduce(np.sum)
<xarray.DataArray (a: 2, b: 4)>
array([[nan, nan, 3., 6.],
[nan, nan, 15., 18.]])
Dimensions without coordinates: a, b
>>>
>>> rolling = da.rolling(b=3, min_periods=1)
>>> rolling.reduce(np.nansum)
<xarray.DataArray (a: 2, b: 4)>
array([[ 0., 1., 3., 6.],
[ 4., 9., 15., 18.]])
"""
rolling_dim = utils.get_temp_dimname(self.obj.dims, '_rolling_dim')
windows = self.construct(rolling_dim)
Expand Down