coarsen

UnitsAwareDataArray.coarsen(dim: Mapping[Any, int] | None = None, boundary: CoarsenBoundaryOptions = 'exact', side: SideOptions | Mapping[Any, SideOptions] = 'left', coord_func: str | Callable | Mapping[Any, str | Callable] = 'mean', **window_kwargs: int) DataArrayCoarsen

Coarsen object for DataArrays.

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"}, default: "left")

  • 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).

Return type:

core.rolling.DataArrayCoarsen

Examples

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

>>> da = xr.DataArray(
...     np.linspace(0, 364, num=364),
...     dims="time",
...     coords={"time": pd.date_range("1999-12-15", periods=364)},
... )
>>> da  # +doctest: ELLIPSIS
<xarray.DataArray (time: 364)> Size: 3kB
array([  0.        ,   1.00275482,   2.00550964,   3.00826446,
         4.01101928,   5.0137741 ,   6.01652893,   7.01928375,
         8.02203857,   9.02479339,  10.02754821,  11.03030303,
        12.03305785,  13.03581267,  14.03856749,  15.04132231,
        16.04407713,  17.04683196,  18.04958678,  19.0523416 ,
        20.05509642,  21.05785124,  22.06060606,  23.06336088,
        24.0661157 ,  25.06887052,  26.07162534,  27.07438017,
        28.07713499,  29.07988981,  30.08264463,  31.08539945,
        32.08815427,  33.09090909,  34.09366391,  35.09641873,
        36.09917355,  37.10192837,  38.1046832 ,  39.10743802,
        40.11019284,  41.11294766,  42.11570248,  43.1184573 ,
        44.12121212,  45.12396694,  46.12672176,  47.12947658,
        48.1322314 ,  49.13498623,  50.13774105,  51.14049587,
        52.14325069,  53.14600551,  54.14876033,  55.15151515,
        56.15426997,  57.15702479,  58.15977961,  59.16253444,
        60.16528926,  61.16804408,  62.1707989 ,  63.17355372,
        64.17630854,  65.17906336,  66.18181818,  67.184573  ,
        68.18732782,  69.19008264,  70.19283747,  71.19559229,
        72.19834711,  73.20110193,  74.20385675,  75.20661157,
        76.20936639,  77.21212121,  78.21487603,  79.21763085,
...
       284.78236915, 285.78512397, 286.78787879, 287.79063361,
       288.79338843, 289.79614325, 290.79889807, 291.80165289,
       292.80440771, 293.80716253, 294.80991736, 295.81267218,
       296.815427  , 297.81818182, 298.82093664, 299.82369146,
       300.82644628, 301.8292011 , 302.83195592, 303.83471074,
       304.83746556, 305.84022039, 306.84297521, 307.84573003,
       308.84848485, 309.85123967, 310.85399449, 311.85674931,
       312.85950413, 313.86225895, 314.86501377, 315.8677686 ,
       316.87052342, 317.87327824, 318.87603306, 319.87878788,
       320.8815427 , 321.88429752, 322.88705234, 323.88980716,
       324.89256198, 325.8953168 , 326.89807163, 327.90082645,
       328.90358127, 329.90633609, 330.90909091, 331.91184573,
       332.91460055, 333.91735537, 334.92011019, 335.92286501,
       336.92561983, 337.92837466, 338.93112948, 339.9338843 ,
       340.93663912, 341.93939394, 342.94214876, 343.94490358,
       344.9476584 , 345.95041322, 346.95316804, 347.95592287,
       348.95867769, 349.96143251, 350.96418733, 351.96694215,
       352.96969697, 353.97245179, 354.97520661, 355.97796143,
       356.98071625, 357.98347107, 358.9862259 , 359.98898072,
       360.99173554, 361.99449036, 362.99724518, 364.        ])
Coordinates:
  * time     (time) datetime64[ns] 3kB 1999-12-15 1999-12-16 ... 2000-12-12
>>> da.coarsen(time=3, boundary="trim").mean()  # +doctest: ELLIPSIS
<xarray.DataArray (time: 121)> Size: 968B
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,
        37.10192837,  40.11019284,  43.1184573 ,  46.12672176,
        49.13498623,  52.14325069,  55.15151515,  58.15977961,
        61.16804408,  64.17630854,  67.184573  ,  70.19283747,
        73.20110193,  76.20936639,  79.21763085,  82.22589532,
        85.23415978,  88.24242424,  91.25068871,  94.25895317,
        97.26721763, 100.27548209, 103.28374656, 106.29201102,
       109.30027548, 112.30853994, 115.31680441, 118.32506887,
       121.33333333, 124.3415978 , 127.34986226, 130.35812672,
       133.36639118, 136.37465565, 139.38292011, 142.39118457,
       145.39944904, 148.4077135 , 151.41597796, 154.42424242,
       157.43250689, 160.44077135, 163.44903581, 166.45730028,
       169.46556474, 172.4738292 , 175.48209366, 178.49035813,
       181.49862259, 184.50688705, 187.51515152, 190.52341598,
       193.53168044, 196.5399449 , 199.54820937, 202.55647383,
       205.56473829, 208.57300275, 211.58126722, 214.58953168,
       217.59779614, 220.60606061, 223.61432507, 226.62258953,
       229.63085399, 232.63911846, 235.64738292, 238.65564738,
       241.66391185, 244.67217631, 247.68044077, 250.68870523,
       253.6969697 , 256.70523416, 259.71349862, 262.72176309,
       265.73002755, 268.73829201, 271.74655647, 274.75482094,
       277.7630854 , 280.77134986, 283.77961433, 286.78787879,
       289.79614325, 292.80440771, 295.81267218, 298.82093664,
       301.8292011 , 304.83746556, 307.84573003, 310.85399449,
       313.86225895, 316.87052342, 319.87878788, 322.88705234,
       325.8953168 , 328.90358127, 331.91184573, 334.92011019,
       337.92837466, 340.93663912, 343.94490358, 346.95316804,
       349.96143251, 352.96969697, 355.97796143, 358.9862259 ,
       361.99449036])
Coordinates:
  * time     (time) datetime64[ns] 968B 1999-12-16 1999-12-19 ... 2000-12-10
>>>

See also

core.rolling.DataArrayCoarsen Dataset.coarsen

Reshaping via coarsen

User guide describing coarsen()

Coarsen large arrays

User guide on block arrgragation coarsen()

xarray-tutorial:fundamentals/03.3_windowed

Tutorial on windowed computation using coarsen()