xarray.DataArray.groupby

DataArray.groupby(self, group, squeeze: bool = True, restore_coord_dims: bool = None)

Returns a GroupBy object for performing grouped operations.

Parameters
  • group (str, DataArray or IndexVariable) – Array whose unique values should be used to group this array. If a string, must be the name of a variable contained in this dataset.

  • squeeze (boolean, optional) – If “group” is a dimension of any arrays in this dataset, squeeze controls whether the subarrays have a dimension of length 1 along that dimension or if the dimension is squeezed out.

  • restore_coord_dims (bool, optional) – If True, also restore the dimension order of multi-dimensional coordinates.

Returns

grouped – A GroupBy object patterned after pandas.GroupBy that can be iterated over in the form of (unique_value, grouped_array) pairs.

Return type

GroupBy

Examples

Calculate daily anomalies for daily data:

>>> da = xr.DataArray(
...     np.linspace(0, 1826, num=1827),
...     coords=[pd.date_range("1/1/2000", "31/12/2004", freq="D")],
...     dims="time",
... )
>>> da
<xarray.DataArray (time: 1827)>
array([0.000e+00, 1.000e+00, 2.000e+00, ..., 1.824e+03, 1.825e+03, 1.826e+03])
Coordinates:
  * time     (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 ...
>>> da.groupby("time.dayofyear") - da.groupby("time.dayofyear").mean("time")
<xarray.DataArray (time: 1827)>
array([-730.8, -730.8, -730.8, ...,  730.2,  730.2,  730.5])
Coordinates:
  * time       (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 ...
    dayofyear  (time) int64 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...