Skip to content

Commit 9e97732

Browse files
authored
Ensure that groupby.groups works even with multiple groupers (#10750)
1 parent 577261d commit 9e97732

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

xarray/core/groupby.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ def groups(self) -> dict[GroupKey, GroupIndex]:
828828
self._groups = dict(
829829
zip(
830830
self.encoded.unique_coord.data,
831-
self.encoded.group_indices,
831+
tuple(g for g in self.encoded.group_indices if g),
832832
strict=True,
833833
)
834834
)

xarray/tests/test_groupby.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3200,6 +3200,16 @@ def test_multiple_grouper_unsorted_order() -> None:
32003200
assert_identical(actual2, expected2)
32013201

32023202

3203+
def test_multiple_grouper_empty_groups() -> None:
3204+
ds = xr.Dataset(
3205+
{"foo": (("x", "y"), np.random.rand(4, 3))},
3206+
coords={"x": [10, 20, 30, 40], "letters": ("x", list("abba"))},
3207+
)
3208+
3209+
groups = ds.groupby(x=BinGrouper(bins=[5, 15, 25]), letters=UniqueGrouper())
3210+
assert len(groups.groups) == 2
3211+
3212+
32033213
def test_groupby_multiple_bin_grouper_missing_groups() -> None:
32043214
from numpy import nan
32053215

0 commit comments

Comments
 (0)