Skip to content

Commit c7c4aae

Browse files
dcherianmathause
andauthored
Implement GroupBy.__getitem__ (#3691)
* Implement GroupBy.__getitem__ * fix test name * move whats new entry Co-authored-by: Mathias Hauser <mathias.hauser@env.ethz.ch>
1 parent d4b7a60 commit c7c4aae

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

doc/groupby.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ You can also iterate over groups in ``(label, group)`` pairs:
6363
6464
list(ds.groupby("letters"))
6565
66+
You can index out a particular group:
67+
68+
.. ipython:: python
69+
70+
ds.groupby("letters")["b"]
71+
6672
Just like in pandas, creating a GroupBy object is cheap: it does not actually
6773
split the data until you access particular values.
6874

doc/whats-new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ New Features
5151
grant from the `Chan Zuckerberg Initiative <https://chanzuckerberg.com>`_ and
5252
developed by `B-Open <https://www.bopen.eu>`_.
5353
By `Aureliana Barghini <https://github.com/aurghs>`_ and `Alessandro Amici <https://github.com/alexamici>`_.
54+
- Implement ``__getitem__`` for both :py:class:`~core.groupby.DatasetGroupBy` and
55+
:py:class:`~core.groupby.DataArrayGroupBy`, inspired by pandas'
56+
:py:meth:`~pandas.core.groupby.GroupBy.get_group`.
57+
By `Deepak Cherian <https://github.com/dcherian>`_.
5458

5559

5660
Breaking changes

xarray/core/groupby.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,20 @@ def dims(self):
415415

416416
@property
417417
def groups(self):
418+
"""
419+
Mapping from group labels to indices. The indices can be used to index the underlying object.
420+
"""
418421
# provided to mimic pandas.groupby
419422
if self._groups is None:
420423
self._groups = dict(zip(self._unique_coord.values, self._group_indices))
421424
return self._groups
422425

426+
def __getitem__(self, key):
427+
"""
428+
Get DataArray or Dataset corresponding to a particular group label.
429+
"""
430+
return self._obj.isel({self._group_dim: self.groups[key]})
431+
423432
def __len__(self):
424433
return self._unique_coord.size
425434

xarray/tests/test_groupby.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,4 +549,17 @@ def test_groupby_none_group_name():
549549
assert "group" in mean.dims
550550

551551

552+
def test_groupby_getitem(dataset):
553+
554+
assert_identical(dataset.sel(x="a"), dataset.groupby("x")["a"])
555+
assert_identical(dataset.sel(z=1), dataset.groupby("z")[1])
556+
557+
assert_identical(dataset.foo.sel(x="a"), dataset.foo.groupby("x")["a"])
558+
assert_identical(dataset.foo.sel(z=1), dataset.foo.groupby("z")[1])
559+
560+
actual = dataset.groupby("boo")["f"].unstack().transpose("x", "y", "z")
561+
expected = dataset.sel(y=[1], z=[1, 2]).transpose("x", "y", "z")
562+
assert_identical(expected, actual)
563+
564+
552565
# TODO: move other groupby tests from test_dataset and test_dataarray over here

0 commit comments

Comments
 (0)