155
155
DsCompatible ,
156
156
ErrorOptions ,
157
157
ErrorOptionsWithWarn ,
158
+ GroupInput ,
158
159
InterpOptions ,
159
160
JoinOptions ,
160
161
PadModeOptions ,
@@ -10332,9 +10333,7 @@ def interp_calendar(
10332
10333
@_deprecate_positional_args ("v2024.07.0" )
10333
10334
def groupby (
10334
10335
self ,
10335
- group : (
10336
- Hashable | DataArray | IndexVariable | Mapping [Any , Grouper ] | None
10337
- ) = None ,
10336
+ group : GroupInput = None ,
10338
10337
* ,
10339
10338
squeeze : Literal [False ] = False ,
10340
10339
restore_coord_dims : bool = False ,
@@ -10344,7 +10343,7 @@ def groupby(
10344
10343
10345
10344
Parameters
10346
10345
----------
10347
- group : Hashable or DataArray or IndexVariable or mapping of Hashable to Grouper
10346
+ group : str or DataArray or IndexVariable or sequence of hashable or mapping of hashable to Grouper
10348
10347
Array whose unique values should be used to group this array. If a
10349
10348
Hashable, must be the name of a coordinate contained in this dataarray. If a dictionary,
10350
10349
must map an existing variable name to a :py:class:`Grouper` instance.
@@ -10366,6 +10365,51 @@ def groupby(
10366
10365
A `DatasetGroupBy` object patterned after `pandas.GroupBy` that can be
10367
10366
iterated over in the form of `(unique_value, grouped_array)` pairs.
10368
10367
10368
+ Examples
10369
+ --------
10370
+ >>> ds = xr.Dataset(
10371
+ ... {"foo": (("x", "y"), np.arange(12).reshape((4, 3)))},
10372
+ ... coords={"x": [10, 20, 30, 40], "letters": ("x", list("abba"))},
10373
+ ... )
10374
+
10375
+ Grouping by a single variable is easy
10376
+
10377
+ >>> ds.groupby("letters")
10378
+ <DatasetGroupBy, grouped over 1 grouper(s), 2 groups in total:
10379
+ 'letters': 2 groups with labels 'a', 'b'>
10380
+
10381
+ Execute a reduction
10382
+
10383
+ >>> ds.groupby("letters").sum()
10384
+ <xarray.Dataset> Size: 64B
10385
+ Dimensions: (letters: 2, y: 3)
10386
+ Coordinates:
10387
+ * letters (letters) object 16B 'a' 'b'
10388
+ Dimensions without coordinates: y
10389
+ Data variables:
10390
+ foo (letters, y) float64 48B 9.0 11.0 13.0 9.0 11.0 13.0
10391
+
10392
+ Grouping by multiple variables
10393
+
10394
+ >>> ds.groupby(["letters", "x"])
10395
+ <DatasetGroupBy, grouped over 2 grouper(s), 8 groups in total:
10396
+ 'letters': 2 groups with labels 'a', 'b'
10397
+ 'x': 4 groups with labels 10, 20, 30, 40>
10398
+
10399
+ Use Grouper objects to express more complicated GroupBy operations
10400
+
10401
+ >>> from xarray.groupers import BinGrouper, UniqueGrouper
10402
+ >>>
10403
+ >>> ds.groupby(x=BinGrouper(bins=[5, 15, 25]), letters=UniqueGrouper()).sum()
10404
+ <xarray.Dataset> Size: 128B
10405
+ Dimensions: (y: 3, x_bins: 2, letters: 2)
10406
+ Coordinates:
10407
+ * x_bins (x_bins) object 16B (5, 15] (15, 25]
10408
+ * letters (letters) object 16B 'a' 'b'
10409
+ Dimensions without coordinates: y
10410
+ Data variables:
10411
+ foo (y, x_bins, letters) float64 96B 0.0 nan nan 3.0 ... nan nan 5.0
10412
+
10369
10413
See Also
10370
10414
--------
10371
10415
:ref:`groupby`
@@ -10387,31 +10431,12 @@ def groupby(
10387
10431
"""
10388
10432
from xarray .core .groupby import (
10389
10433
DatasetGroupBy ,
10390
- ResolvedGrouper ,
10434
+ _parse_group_and_groupers ,
10391
10435
_validate_groupby_squeeze ,
10392
10436
)
10393
- from xarray .groupers import UniqueGrouper
10394
10437
10395
10438
_validate_groupby_squeeze (squeeze )
10396
-
10397
- if isinstance (group , Mapping ):
10398
- groupers = either_dict_or_kwargs (group , groupers , "groupby" ) # type: ignore
10399
- group = None
10400
-
10401
- rgroupers : tuple [ResolvedGrouper , ...]
10402
- if group is not None :
10403
- if groupers :
10404
- raise ValueError (
10405
- "Providing a combination of `group` and **groupers is not supported."
10406
- )
10407
- rgroupers = (ResolvedGrouper (UniqueGrouper (), group , self ),)
10408
- else :
10409
- if not groupers :
10410
- raise ValueError ("Either `group` or `**groupers` must be provided." )
10411
- rgroupers = tuple (
10412
- ResolvedGrouper (grouper , group , self )
10413
- for group , grouper in groupers .items ()
10414
- )
10439
+ rgroupers = _parse_group_and_groupers (self , group , groupers )
10415
10440
10416
10441
return DatasetGroupBy (self , rgroupers , restore_coord_dims = restore_coord_dims )
10417
10442
0 commit comments