xarray.core.rolling.DataArrayRolling.construct

DataArrayRolling.construct(window_dim, stride=1, fill_value=<NA>)

Convert this rolling object to xr.DataArray, where the window dimension is stacked as a new dimension

Parameters:
window_dim: str

New name of the window dimension.

stride: integer, optional

Size of stride for the rolling window.

fill_value: optional. Default dtypes.NA

Filling value to match the dimension size.

Returns:
DataArray that is a view of the original array. The returned array is
not writeable.

Examples

>>> da = DataArray(np.arange(8).reshape(2, 4), dims=('a', 'b'))
>>>
>>> rolling = da.rolling(a=3)
>>> rolling.to_datarray('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 = da.rolling(a=3, center=True)
>>> rolling.to_datarray('window_dim')
<xarray.DataArray (a: 2, b: 4, window_dim: 3)>
array([[[np.nan, 0, 1], [0, 1, 2], [1, 2, 3], [2, 3, np.nan]],
       [[np.nan, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, np.nan]]])
Dimensions without coordinates: a, b, window_dim