xarray.DataArray.coarsen

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

Coarsen object.

Parameters
  • dim (mapping of hashable to int, optional) – Mapping from the dimension name to the window size.

  • boundary ({"exact", "trim", "pad"}, default: "exact") – 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", "right"} or mapping of str to {"left", "right"})

  • coord_func (str or mapping of hashable to str, default: "mean") – function (name) that is applied to the coordinates, or a mapping from coordinate name to function (name).

  • keep_attrs (bool, optional) – If True, the object’s attributes (attrs) will be copied from the original object to the new one. If False (default), the new object will be returned without attributes.

Returns

core.rolling.DataArrayCoarsen or core.rolling.DatasetCoarsen – A coarsen object (DataArrayCoarsen for DataArray, 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  # +doctest: ELLIPSIS
<xarray.DataArray (time: 364)>
array([  0.        ,   1.00275482,   2.00550964,   3.00826446,
         4.01101928,   5.0137741 ,   6.01652893,   7.01928375,
         8.02203857,   9.02479339,  10.02754821,  11.03030303,
...
       356.98071625, 357.98347107, 358.9862259 , 359.98898072,
       360.99173554, 361.99449036, 362.99724518, 364.        ])
Coordinates:
  * time     (time) datetime64[ns] 1999-12-15 1999-12-16 ... 2000-12-12
>>> da.coarsen(time=3, boundary="trim").mean()  # +doctest: ELLIPSIS
<xarray.DataArray (time: 121)>
array([  1.00275482,   4.01101928,   7.01928375,  10.02754821,
        13.03581267,  16.04407713,  19.0523416 ,  22.06060606,
        25.06887052,  28.07713499,  31.08539945,  34.09366391,
...
       349.96143251, 352.96969697, 355.97796143, 358.9862259 ,
       361.99449036])
Coordinates:
  * time     (time) datetime64[ns] 1999-12-16 1999-12-19 ... 2000-12-10
>>>