xarray.DataArray.coarsen

DataArray.coarsen(dim=None, boundary='exact', side='left', coord_func='mean', **window_kwargs)

Coarsen object.

Parameters
  • dim (dict, optional) –

    Mapping from the dimension name to the window size. dim : str

    Name of the dimension to create the rolling iterator along (e.g., time).

    windowint

    Size of the moving window.

  • boundary ('exact' | 'trim' | 'pad') – If ‘exact’, a ValueError will be raised if dimension size is not a multiple of the window size. If ‘trim’, the excess entries are dropped. If ‘pad’, NA will be padded.

  • side ('left' or 'right' or mapping from dimension to 'left' or 'right') –

  • coord_func (function (name) that is applied to the coordintes,) – or a mapping from coordinate name to function (name).

Returns

  • Coarsen object (core.rolling.DataArrayCoarsen for DataArray,

  • core.rolling.DatasetCoarsen for Dataset.)

Examples

Coarsen the long time series by averaging over every four days.

>>> da = xr.DataArray(np.linspace(0, 364, num=364),
...                   dims='time',
...                   coords={'time': pd.date_range(
...                       '15/12/1999', periods=364)})
>>> da
<xarray.DataArray (time: 364)>
array([  0.      ,   1.002755,   2.00551 , ..., 361.99449 , 362.997245,
       364.      ])
Coordinates:
  * time     (time) datetime64[ns] 1999-12-15 1999-12-16 ... 2000-12-12
>>>
>>> da.coarsen(time=3, boundary='trim').mean()
<xarray.DataArray (time: 121)>
array([  1.002755,   4.011019,   7.019284,  ...,  358.986226,
       361.99449 ])
Coordinates:
  * time     (time) datetime64[ns] 1999-12-16 1999-12-19 ... 2000-12-10
>>>

See also

core.rolling.DataArrayCoarsen(), core.rolling.DatasetCoarsen()