Returns a DataArrayGroupBy object for performing grouped operations. Parameters ---------- group : str or DataArray or IndexVariable or sequence of hashable or mapping of hashable to Grouper Array whose unique values should be used to group this array. If a
(
self,
group: GroupInput = None,
*,
squeeze: Literal[False] = False,
restore_coord_dims: bool = False,
eagerly_compute_group: Literal[False] | None = None,
**groupers: Grouper,
)
| 6929 | |
| 6930 | @_deprecate_positional_args("v2024.07.0") |
| 6931 | def groupby( |
| 6932 | self, |
| 6933 | group: GroupInput = None, |
| 6934 | *, |
| 6935 | squeeze: Literal[False] = False, |
| 6936 | restore_coord_dims: bool = False, |
| 6937 | eagerly_compute_group: Literal[False] | None = None, |
| 6938 | **groupers: Grouper, |
| 6939 | ) -> DataArrayGroupBy: |
| 6940 | """Returns a DataArrayGroupBy object for performing grouped operations. |
| 6941 | |
| 6942 | Parameters |
| 6943 | ---------- |
| 6944 | group : str or DataArray or IndexVariable or sequence of hashable or mapping of hashable to Grouper |
| 6945 | Array whose unique values should be used to group this array. If a |
| 6946 | Hashable, must be the name of a coordinate contained in this dataarray. If a dictionary, |
| 6947 | must map an existing variable name to a :py:class:`Grouper` instance. |
| 6948 | squeeze : False |
| 6949 | This argument is deprecated. |
| 6950 | restore_coord_dims : bool, default: False |
| 6951 | If True, also restore the dimension order of multi-dimensional |
| 6952 | coordinates. |
| 6953 | eagerly_compute_group: bool, optional |
| 6954 | This argument is deprecated. |
| 6955 | **groupers : Mapping of str to Grouper or Resampler |
| 6956 | Mapping of variable name to group by to :py:class:`Grouper` or :py:class:`Resampler` object. |
| 6957 | One of ``group`` or ``groupers`` must be provided. |
| 6958 | Only a single ``grouper`` is allowed at present. |
| 6959 | |
| 6960 | Returns |
| 6961 | ------- |
| 6962 | grouped : DataArrayGroupBy |
| 6963 | A `DataArrayGroupBy` object patterned after `pandas.GroupBy` that can be |
| 6964 | iterated over in the form of `(unique_value, grouped_array)` pairs. |
| 6965 | |
| 6966 | Examples |
| 6967 | -------- |
| 6968 | Calculate daily anomalies for daily data: |
| 6969 | |
| 6970 | >>> da = xr.DataArray( |
| 6971 | ... np.linspace(0, 1826, num=1827), |
| 6972 | ... coords=[pd.date_range("2000-01-01", "2004-12-31", freq="D")], |
| 6973 | ... dims="time", |
| 6974 | ... ) |
| 6975 | >>> da |
| 6976 | <xarray.DataArray (time: 1827)> Size: 15kB |
| 6977 | array([0.000e+00, 1.000e+00, 2.000e+00, ..., 1.824e+03, 1.825e+03, |
| 6978 | 1.826e+03], shape=(1827,)) |
| 6979 | Coordinates: |
| 6980 | * time (time) datetime64[us] 15kB 2000-01-01 2000-01-02 ... 2004-12-31 |
| 6981 | >>> da.groupby("time.dayofyear") - da.groupby("time.dayofyear").mean("time") |
| 6982 | <xarray.DataArray (time: 1827)> Size: 15kB |
| 6983 | array([-730.8, -730.8, -730.8, ..., 730.2, 730.2, 730.5], shape=(1827,)) |
| 6984 | Coordinates: |
| 6985 | * time (time) datetime64[us] 15kB 2000-01-01 2000-01-02 ... 2004-12-31 |
| 6986 | dayofyear (time) int64 15kB 1 2 3 4 5 6 7 8 ... 360 361 362 363 364 365 366 |
| 6987 | |
| 6988 | Use a ``Grouper`` object to be more explicit |