# xarray.computation.rolling.DataArrayRolling.reduce

#### DataArrayRolling.reduce(func, keep_attrs=None, , sliding_window_view_kwargs=None, \*\*kwargs)[[source]](https://github.com/pydata/xarray/blob/main/xarray/computation/rolling.py#L522-L626)

Reduce each window by applying func.

Equivalent to `.construct(...).reduce(func, ...)`.

* **Parameters:**
  * **func** ([`callable()`](https://docs.python.org/3/builtins/functions.html#callable)) – Function which can be called in the form
    func(x, \*\*kwargs) to return the result of collapsing an
    np.ndarray over the rolling dimension.
  * **keep_attrs** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), *default*: [`None`](https://docs.python.org/3/builtins/constants.html#None)) – If True, the attributes (`attrs`) will be copied from the original
    object to the new one. If False, the new object will be returned
    without attributes. If None uses the global default.
  * **sliding_window_view_kwargs** – Keyword arguments that should be passed to the underlying array type’s
    `sliding_window_view` function.
  * **\*\*kwargs** ([`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)) – Additional keyword arguments passed on to func.
* **Returns:**
  **reduced** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray)) – Array with summarized data.

#### SEE ALSO
[`numpy.lib.stride_tricks.sliding_window_view`](https://numpy.org/doc/stable/reference/generated/numpy.lib.stride_tricks.sliding_window_view.html#numpy.lib.stride_tricks.sliding_window_view), [`dask.array.lib.stride_tricks.sliding_window_view`](https://docs.dask.org/en/latest/generated/dask.array.lib.stride_tricks.sliding_window_view.html#dask.array.lib.stride_tricks.sliding_window_view)

### Notes

With dask arrays, it’s possible to pass the `automatic_rechunk` kwarg as
`sliding_window_view_kwargs={"automatic_rechunk": True}`. This controls
whether dask should automatically rechunk the output to avoid
exploding chunk sizes. Automatically rechunking is the default behaviour.
Importantly, each chunk will be a view of the data so large chunk sizes are
only safe if *no* copies are made later.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('428e9e1b-e8c7-43a3-88b0-c00273350132','d0352ae1-e981-4d72-a5c2-ab5247de912e','ef3d8bd7-d2de-4b77-86d2-c30746707262','../lite/tree/../notebooks/index.html?path=1f3d9e8e_dd79_4113_9f27_2adb624ef853.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> da = xr.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)> Size: 192B
array([[[nan, nan,  0.],
        [nan,  0.,  1.],
        [ 0.,  1.,  2.],
        [ 1.,  2.,  3.]],

       [[nan, nan,  4.],
        [nan,  4.,  5.],
        [ 4.,  5.,  6.],
        [ 5.,  6.,  7.]]])
Dimensions without coordinates: a, b, window_dim
```

```pycon
>>> rolling.reduce(np.sum)
<xarray.DataArray (a: 2, b: 4)> Size: 64B
array([[nan, nan,  3.,  6.],
       [nan, nan, 15., 18.]])
Dimensions without coordinates: a, b
```

```pycon
>>> rolling = da.rolling(b=3, min_periods=1)
>>> rolling.reduce(np.nansum)
<xarray.DataArray (a: 2, b: 4)> Size: 64B
array([[ 0.,  1.,  3.,  6.],
       [ 4.,  9., 15., 18.]])
Dimensions without coordinates: a, b
```

<div id="ef3d8bd7-d2de-4b77-86d2-c30746707262" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('428e9e1b-e8c7-43a3-88b0-c00273350132','ef3d8bd7-d2de-4b77-86d2-c30746707262')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('428e9e1b-e8c7-43a3-88b0-c00273350132','ef3d8bd7-d2de-4b77-86d2-c30746707262')">Open In Tab</button></div><div id="d0352ae1-e981-4d72-a5c2-ab5247de912e" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
