Skip to content

Commit c8affb3

Browse files
authored
Fix BinGrouper when labels is not specified (#10295)
1 parent dd9a46b commit c8affb3

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Deprecations
3636

3737
Bug fixes
3838
~~~~~~~~~
39+
- Fix :py:class:`~xarray.groupers.BinGrouper` when ``labels`` is not specified (:issue:`10284`).
40+
By `Deepak Cherian <https://github.com/dcherian>`_.
3941

4042
- Allow accessing arbitrary attributes on Pandas ExtensionArrays.
4143
By `Deepak Cherian <https://github.com/dcherian>`_.

xarray/groupers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def factorize(self, group: T_Group) -> EncodedGroups:
413413
# This seems silly, but it lets us have Pandas handle the complexity
414414
# of `labels`, `precision`, and `include_lowest`, even when group is a chunked array
415415
# Pandas ignores labels when IntervalIndex is passed
416-
if not isinstance(self.bins, pd.IntervalIndex):
416+
if self.labels is None or not isinstance(self.bins, pd.IntervalIndex):
417417
dummy, _ = self._cut(np.array([0]).astype(group.dtype))
418418
full_index = dummy.categories
419419
else:

xarray/tests/test_groupby.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,10 +1042,12 @@ def test_groupby_math_bitshift() -> None:
10421042
assert_equal(right_expected, right_actual)
10431043

10441044

1045+
@pytest.mark.parametrize(
1046+
"x_bins", ((0, 2, 4, 6), pd.IntervalIndex.from_breaks((0, 2, 4, 6), closed="left"))
1047+
)
10451048
@pytest.mark.parametrize("use_flox", [True, False])
1046-
def test_groupby_bins_cut_kwargs(use_flox: bool) -> None:
1049+
def test_groupby_bins_cut_kwargs(use_flox: bool, x_bins) -> None:
10471050
da = xr.DataArray(np.arange(12).reshape(6, 2), dims=("x", "y"))
1048-
x_bins = (0, 2, 4, 6)
10491051

10501052
with xr.set_options(use_flox=use_flox):
10511053
actual = da.groupby_bins(
@@ -1055,7 +1057,12 @@ def test_groupby_bins_cut_kwargs(use_flox: bool) -> None:
10551057
np.array([[1.0, 2.0], [5.0, 6.0], [9.0, 10.0]]),
10561058
dims=("x_bins", "y"),
10571059
coords={
1058-
"x_bins": ("x_bins", pd.IntervalIndex.from_breaks(x_bins, closed="left"))
1060+
"x_bins": (
1061+
"x_bins",
1062+
x_bins
1063+
if isinstance(x_bins, pd.IntervalIndex)
1064+
else pd.IntervalIndex.from_breaks(x_bins, closed="left"),
1065+
)
10591066
},
10601067
)
10611068
assert_identical(expected, actual)
@@ -1067,9 +1074,8 @@ def test_groupby_bins_cut_kwargs(use_flox: bool) -> None:
10671074
assert_identical(expected, actual)
10681075

10691076
with xr.set_options(use_flox=use_flox):
1070-
bins_index = pd.IntervalIndex.from_breaks(x_bins)
10711077
labels = ["one", "two", "three"]
1072-
actual = da.groupby(x=BinGrouper(bins=bins_index, labels=labels)).sum()
1078+
actual = da.groupby(x=BinGrouper(bins=x_bins, labels=labels)).sum()
10731079
assert actual.xindexes["x_bins"].index.equals(pd.Index(labels)) # type: ignore[attr-defined]
10741080

10751081

0 commit comments

Comments
 (0)