xarray.core.rolling.DataArrayRolling.reduce

DataArrayRolling.reduce(func, **kwargs)

Reduce the items in this group by applying func along some dimension(s).

Parameters
  • func (function) – Function which can be called in the form func(x, **kwargs) to return the result of collapsing an np.ndarray over an the rolling dimension.

  • **kwargs (dict) – Additional keyword arguments passed on to func.

Returns

reduced – Array with summarized data.

Return type

DataArray

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.]])