@@ -81,8 +81,7 @@ You can index out a particular group:
8181
8282 ds.groupby(" letters" )[" b" ]
8383
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 >`.
8685
8786Binning
8887~~~~~~~
@@ -180,19 +179,6 @@ This last line is roughly equivalent to the following::
180179 results.append(group - alt.sel(letters=label))
181180 xr.concat(results, dim='x')
182181
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-
196182.. _groupby.multidim :
197183
198184Multidimensional Grouping
@@ -236,6 +222,8 @@ applying your function, and then unstacking the result:
236222 stacked = da.stack(gridcell = [" ny" , " nx" ])
237223 stacked.groupby(" gridcell" ).sum(... ).unstack(" gridcell" )
238224
225+ Alternatively, you can groupby both `lat ` and `lon ` at the :ref: `same time <groupby.multiple >`.
226+
239227.. _groupby.groupers :
240228
241229Grouper Objects
@@ -276,7 +264,8 @@ is identical to
276264
277265 ds.groupby(x = UniqueGrouper())
278266
279- and
267+
268+ Similarly,
280269
281270.. code-block :: python
282271
@@ -303,3 +292,26 @@ is identical to
303292 from xarray.groupers import TimeResampler
304293
305294 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