Reduce each window by applying `func`. Equivalent to ``.construct(...).reduce(func, ...)``. Parameters ---------- func : callable Function which can be called in the form `func(x, **kwargs)` to return the result of collapsing an n
(
self,
func: Callable,
keep_attrs: bool | None = None,
*,
sliding_window_view_kwargs: Mapping[Any, Any] | None = None,
**kwargs: Any,
)
| 475 | ) |
| 476 | |
| 477 | def reduce( |
| 478 | self, |
| 479 | func: Callable, |
| 480 | keep_attrs: bool | None = None, |
| 481 | *, |
| 482 | sliding_window_view_kwargs: Mapping[Any, Any] | None = None, |
| 483 | **kwargs: Any, |
| 484 | ) -> DataArray: |
| 485 | """Reduce each window by applying `func`. |
| 486 | |
| 487 | Equivalent to ``.construct(...).reduce(func, ...)``. |
| 488 | |
| 489 | Parameters |
| 490 | ---------- |
| 491 | func : callable |
| 492 | Function which can be called in the form |
| 493 | `func(x, **kwargs)` to return the result of collapsing an |
| 494 | np.ndarray over the rolling dimension. |
| 495 | keep_attrs : bool, default: None |
| 496 | If True, the attributes (``attrs``) will be copied from the original |
| 497 | object to the new one. If False, the new object will be returned |
| 498 | without attributes. If None uses the global default. |
| 499 | sliding_window_view_kwargs |
| 500 | Keyword arguments that should be passed to the underlying array type's |
| 501 | ``sliding_window_view`` function. |
| 502 | **kwargs : dict |
| 503 | Additional keyword arguments passed on to `func`. |
| 504 | |
| 505 | Returns |
| 506 | ------- |
| 507 | reduced : DataArray |
| 508 | Array with summarized data. |
| 509 | |
| 510 | See Also |
| 511 | -------- |
| 512 | numpy.lib.stride_tricks.sliding_window_view |
| 513 | dask.array.lib.stride_tricks.sliding_window_view |
| 514 | |
| 515 | Notes |
| 516 | ----- |
| 517 | With dask arrays, it's possible to pass the ``automatic_rechunk`` kwarg as |
| 518 | ``sliding_window_view_kwargs={"automatic_rechunk": True}``. This controls |
| 519 | whether dask should automatically rechunk the output to avoid |
| 520 | exploding chunk sizes. Automatically rechunking is the default behaviour. |
| 521 | Importantly, each chunk will be a view of the data so large chunk sizes are |
| 522 | only safe if *no* copies are made later. |
| 523 | |
| 524 | Examples |
| 525 | -------- |
| 526 | >>> da = xr.DataArray(np.arange(8).reshape(2, 4), dims=("a", "b")) |
| 527 | >>> rolling = da.rolling(b=3) |
| 528 | >>> rolling.construct("window_dim") |
| 529 | <xarray.DataArray (a: 2, b: 4, window_dim: 3)> Size: 192B |
| 530 | array([[[nan, nan, 0.], |
| 531 | [nan, 0., 1.], |
| 532 | [ 0., 1., 2.], |
| 533 | [ 1., 2., 3.]], |
| 534 | <BLANKLINE> |
no test coverage detected