Returns a DatasetGroupBy 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,
)
| 10111 | |
| 10112 | @_deprecate_positional_args("v2024.07.0") |
| 10113 | def groupby( |
| 10114 | self, |
| 10115 | group: GroupInput = None, |
| 10116 | *, |
| 10117 | squeeze: Literal[False] = False, |
| 10118 | restore_coord_dims: bool = False, |
| 10119 | eagerly_compute_group: Literal[False] | None = None, |
| 10120 | **groupers: Grouper, |
| 10121 | ) -> DatasetGroupBy: |
| 10122 | """Returns a DatasetGroupBy object for performing grouped operations. |
| 10123 | |
| 10124 | Parameters |
| 10125 | ---------- |
| 10126 | group : str or DataArray or IndexVariable or sequence of hashable or mapping of hashable to Grouper |
| 10127 | Array whose unique values should be used to group this array. If a |
| 10128 | Hashable, must be the name of a coordinate contained in this dataarray. If a dictionary, |
| 10129 | must map an existing variable name to a :py:class:`Grouper` instance. |
| 10130 | squeeze : False |
| 10131 | This argument is deprecated. |
| 10132 | restore_coord_dims : bool, default: False |
| 10133 | If True, also restore the dimension order of multi-dimensional |
| 10134 | coordinates. |
| 10135 | eagerly_compute_group: False, optional |
| 10136 | This argument is deprecated. |
| 10137 | **groupers : Mapping of str to Grouper or Resampler |
| 10138 | Mapping of variable name to group by to :py:class:`Grouper` or :py:class:`Resampler` object. |
| 10139 | One of ``group`` or ``groupers`` must be provided. |
| 10140 | Only a single ``grouper`` is allowed at present. |
| 10141 | |
| 10142 | Returns |
| 10143 | ------- |
| 10144 | grouped : DatasetGroupBy |
| 10145 | A `DatasetGroupBy` object patterned after `pandas.GroupBy` that can be |
| 10146 | iterated over in the form of `(unique_value, grouped_array)` pairs. |
| 10147 | |
| 10148 | Examples |
| 10149 | -------- |
| 10150 | >>> ds = xr.Dataset( |
| 10151 | ... {"foo": (("x", "y"), np.arange(12).reshape((4, 3)))}, |
| 10152 | ... coords={"x": [10, 20, 30, 40], "letters": ("x", list("abba"))}, |
| 10153 | ... ) |
| 10154 | |
| 10155 | Grouping by a single variable is easy |
| 10156 | |
| 10157 | >>> ds.groupby("letters") |
| 10158 | <DatasetGroupBy, grouped over 1 grouper(s), 2 groups in total: |
| 10159 | 'letters': UniqueGrouper('letters'), 2/2 groups with labels 'a', 'b'> |
| 10160 | |
| 10161 | Execute a reduction |
| 10162 | |
| 10163 | >>> ds.groupby("letters").sum() |
| 10164 | <xarray.Dataset> Size: 64B |
| 10165 | Dimensions: (letters: 2, y: 3) |
| 10166 | Coordinates: |
| 10167 | * letters (letters) object 16B 'a' 'b' |
| 10168 | Dimensions without coordinates: y |
| 10169 | Data variables: |
| 10170 | foo (letters, y) int64 48B 9 11 13 9 11 13 |