Skip to content

Commit f84c514

Browse files
kmsquiredcherian
authored andcommitted
Add examples for DataArrayRolling.reduce() (#2968)
* Add Examples to DataArrayRolling.reduce * Adds two examples to DataArrayRolling.reduce(); the second example shows the interaction of reduce() and min_periods * Update Rolling window operations documentation * Add a longer description and examples for the `center` and `min_period` parameters to `DataArray.rolling()`
1 parent 5b3a41d commit f84c514

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

doc/computation.rst

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,35 @@ name of the dimension as a key (e.g. ``y``) and the window size as the value
149149
150150
arr.rolling(y=3)
151151
152-
The label position and minimum number of periods in the rolling window are
153-
controlled by the ``center`` and ``min_periods`` arguments:
152+
Aggregation and summary methods can be applied directly to the ``Rolling``
153+
object:
154154

155155
.. ipython:: python
156156
157-
arr.rolling(y=3, min_periods=2, center=True)
157+
r = arr.rolling(y=3)
158+
r.reduce(np.std)
159+
r.mean()
158160
159-
Aggregation and summary methods can be applied directly to the ``Rolling`` object:
161+
Aggregation results are assigned the coordinate at the end of each window by
162+
default, but can be centered by passing ``center=True`` when constructing the
163+
``Rolling`` object:
160164

161165
.. ipython:: python
162166
163-
r = arr.rolling(y=3)
167+
r = arr.rolling(y=3, center=True)
168+
r.mean()
169+
170+
As can be seen above, aggregations of windows which overlap the border of the
171+
array produce ``nan``s. Setting ``min_periods`` in the call to ``rolling``
172+
changes the minimum number of observations within the window required to have
173+
a value when aggregating:
174+
175+
.. ipython:: python
176+
177+
r = arr.rolling(y=3, min_periods=2)
178+
r.mean()
179+
r = arr.rolling(y=3, center=True, min_periods=2)
164180
r.mean()
165-
r.reduce(np.std)
166181
167182
Note that rolling window aggregations are faster when bottleneck_ is installed.
168183

xarray/core/rolling.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,29 @@ def reduce(self, func, **kwargs):
211211
-------
212212
reduced : DataArray
213213
Array with summarized data.
214+
215+
Examples
216+
--------
217+
>>> da = DataArray(np.arange(8).reshape(2, 4), dims=('a', 'b'))
218+
>>>
219+
>>> rolling = da.rolling(b=3)
220+
>>> rolling.construct('window_dim')
221+
<xarray.DataArray (a: 2, b: 4, window_dim: 3)>
222+
array([[[np.nan, np.nan, 0], [np.nan, 0, 1], [0, 1, 2], [1, 2, 3]],
223+
[[np.nan, np.nan, 4], [np.nan, 4, 5], [4, 5, 6], [5, 6, 7]]])
224+
Dimensions without coordinates: a, b, window_dim
225+
>>>
226+
>>> rolling.reduce(np.sum)
227+
<xarray.DataArray (a: 2, b: 4)>
228+
array([[nan, nan, 3., 6.],
229+
[nan, nan, 15., 18.]])
230+
Dimensions without coordinates: a, b
231+
>>>
232+
>>> rolling = da.rolling(b=3, min_periods=1)
233+
>>> rolling.reduce(np.nansum)
234+
<xarray.DataArray (a: 2, b: 4)>
235+
array([[ 0., 1., 3., 6.],
236+
[ 4., 9., 15., 18.]])
214237
"""
215238
rolling_dim = utils.get_temp_dimname(self.obj.dims, '_rolling_dim')
216239
windows = self.construct(rolling_dim)

0 commit comments

Comments
 (0)