xarray.core.rolling.DataArrayRolling.construct#
- DataArrayRolling.construct(window_dim=None, *, stride=1, fill_value=<NA>, keep_attrs=None, sliding_window_view_kwargs=None, **window_dim_kwargs)[source]#
Convert this rolling object to xr.DataArray, where the window dimension is stacked as a new dimension
- Parameters:
window_dim (
Hashable
or dict-like toHashable
, optional) – A mapping from dimension name to the new window dimension names.stride (
int
or mapping ofint
, default:1
) – Size of stride for the rolling window.fill_value (default:
dtypes.NA
) – Filling value to match the dimension size.keep_attrs (
bool
, default: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 (
Mapping
) – Keyword arguments that should be passed to the underlying array type’ssliding_window_view
function.**window_dim_kwargs (
Hashable
, optional) – The keyword arguments form ofwindow_dim
{dim: new_name, …}.
- Returns:
DataArray
– a view of the original array. By default, the returned array is not writeable. For numpy arrays, one can passwriteable=True
insliding_window_view_kwargs
.
See also
numpy.lib.stride_tricks.sliding_window_view
,dask.array.lib.stride_tricks.sliding_window_view
Notes
With dask arrays, it’s possible to pass the
automatic_rechunk
kwarg assliding_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
>>> 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
>>> rolling = da.rolling(b=3, center=True) >>> rolling.construct("window_dim") <xarray.DataArray (a: 2, b: 4, window_dim: 3)> Size: 192B array([[[nan, 0., 1.], [ 0., 1., 2.], [ 1., 2., 3.], [ 2., 3., nan]], [[nan, 4., 5.], [ 4., 5., 6.], [ 5., 6., 7.], [ 6., 7., nan]]]) Dimensions without coordinates: a, b, window_dim