@@ -81,8 +81,7 @@ You can index out a particular group:
81
81
82
82
ds.groupby(" letters" )[" b" ]
83
83
84
- Just like in pandas, creating a GroupBy object is cheap: it does not actually
85
- split the data until you access particular values.
84
+ To group by multiple variables, see :ref: `this section <groupby.multiple >`.
86
85
87
86
Binning
88
87
~~~~~~~
@@ -180,19 +179,6 @@ This last line is roughly equivalent to the following::
180
179
results.append(group - alt.sel(letters=label))
181
180
xr.concat(results, dim='x')
182
181
183
- Iterating and Squeezing
184
- ~~~~~~~~~~~~~~~~~~~~~~~
185
-
186
- Previously, Xarray defaulted to squeezing out dimensions of size one when iterating over
187
- a GroupBy object. This behaviour is being removed.
188
- You can always squeeze explicitly later with the Dataset or DataArray
189
- :py:meth: `DataArray.squeeze ` methods.
190
-
191
- .. ipython :: python
192
-
193
- next (iter (arr.groupby(" x" , squeeze = False )))
194
-
195
-
196
182
.. _groupby.multidim :
197
183
198
184
Multidimensional Grouping
@@ -236,6 +222,8 @@ applying your function, and then unstacking the result:
236
222
stacked = da.stack(gridcell = [" ny" , " nx" ])
237
223
stacked.groupby(" gridcell" ).sum(... ).unstack(" gridcell" )
238
224
225
+ Alternatively, you can groupby both `lat ` and `lon ` at the :ref: `same time <groupby.multiple >`.
226
+
239
227
.. _groupby.groupers :
240
228
241
229
Grouper Objects
@@ -276,7 +264,8 @@ is identical to
276
264
277
265
ds.groupby(x = UniqueGrouper())
278
266
279
- and
267
+
268
+ Similarly,
280
269
281
270
.. code-block :: python
282
271
@@ -303,3 +292,26 @@ is identical to
303
292
from xarray.groupers import TimeResampler
304
293
305
294
ds.resample(time = TimeResampler(" ME" ))
295
+
296
+
297
+ .. _groupby.multiple :
298
+
299
+ Grouping by multiple variables
300
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
301
+
302
+ Use grouper objects to group by multiple dimensions:
303
+
304
+ .. ipython :: python
305
+
306
+ from xarray.groupers import UniqueGrouper
307
+
308
+ da.groupby(lat = UniqueGrouper(), lon = UniqueGrouper()).sum()
309
+
310
+
311
+ Different groupers can be combined to construct sophisticated GroupBy operations.
312
+
313
+ .. ipython :: python
314
+
315
+ from xarray.groupers import BinGrouper
316
+
317
+ ds.groupby(x = BinGrouper(bins = [5 , 15 , 25 ]), letters = UniqueGrouper()).sum()
0 commit comments