Skip to content

Commit 4b79a71

Browse files
committed
Add GroupBy.get_group. copying Pandas API
1 parent 8a650a1 commit 4b79a71

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

doc/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,11 @@ GroupBy objects
559559
core.groupby.DataArrayGroupBy
560560
core.groupby.DataArrayGroupBy.map
561561
core.groupby.DataArrayGroupBy.reduce
562+
core.groupby.DataArrayGroupBy.get_group
562563
core.groupby.DatasetGroupBy
563564
core.groupby.DatasetGroupBy.map
564565
core.groupby.DatasetGroupBy.reduce
566+
core.groupby.DatasetGroupBy.get_group
565567

566568
Rolling objects
567569
===============

doc/whats-new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ New Features
3434
- :py:meth:`Dataset.quantile`, :py:meth:`DataArray.quantile` and ``GroupBy.quantile``
3535
now work with dask Variables.
3636
By `Deepak Cherian <https://github.com/dcherian>`_.
37+
- Implement ``get_group`` for both :py:class:`~core.groupby.DatasetGroupBy` and
38+
:py:class:`~core.groupby.DataArrayGroupBy`, inspired by pandas'
39+
:py:meth:`~pandas.core.groupby.GroupBy.get_group`.
40+
By `Deepak Cherian <https://github.com/dcherian>`_.
3741
- Added the ``count`` reduction method to both :py:class:`~core.rolling.DatasetCoarsen`
3842
and :py:class:`~core.rolling.DataArrayCoarsen` objects. (:pull:`3500`)
3943
By `Deepak Cherian <https://github.com/dcherian>`_

xarray/core/groupby.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,11 +426,24 @@ def dims(self):
426426

427427
@property
428428
def groups(self):
429+
"""
430+
Mapping from group labels to indices. The indices can be used to index the underlying object.
431+
"""
429432
# provided to mimic pandas.groupby
430433
if self._groups is None:
431434
self._groups = dict(zip(self._unique_coord.values, self._group_indices))
432435
return self._groups
433436

437+
def get_group(self, name):
438+
"""
439+
Get DataArray or Dataset corresponding to a particular group label.
440+
"""
441+
if name not in self.groups:
442+
raise KeyError(f"{name} is not a valid group label.")
443+
444+
indices = self.groups[name]
445+
return self._obj.isel(**{self._group_dim: indices})
446+
434447
def __len__(self):
435448
return self._unique_coord.size
436449

xarray/tests/test_groupby.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,4 +499,17 @@ def test_groupby_bins_timeseries():
499499
assert_identical(actual, expected)
500500

501501

502+
def test_groupby_get_group(dataset):
503+
504+
assert_identical(dataset.sel(x="a"), dataset.groupby("x").get_group("a"))
505+
assert_identical(dataset.sel(z=1), dataset.groupby("z").get_group(1))
506+
507+
assert_identical(dataset.foo.sel(x="a"), dataset.foo.groupby("x").get_group("a"))
508+
assert_identical(dataset.foo.sel(z=1), dataset.foo.groupby("z").get_group(1))
509+
510+
actual = dataset.groupby("boo").get_group("f").unstack().transpose("x", "y", "z")
511+
expected = dataset.sel(y=[1], z=[1, 2]).transpose("x", "y", "z")
512+
assert_identical(expected, actual)
513+
514+
502515
# TODO: move other groupby tests from test_dataset and test_dataarray over here

0 commit comments

Comments
 (0)