Skip to content

Commit 70266e1

Browse files
committed
fix tests
1 parent 4fb17b1 commit 70266e1

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

xarray/core/groupby.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,7 @@ def _flox_reduce(self, dim, **kwargs):
552552
# TODO: switch to xindexes after we can use is_unique
553553
index = self._obj.indexes[self._group.name]
554554
if index.is_unique and self._squeeze:
555-
raise ValueError(
556-
f"Cannot reduce over absent dimensions {self._group.name!r}"
557-
)
555+
raise ValueError(f"cannot reduce over dimensions {self._group.name!r}")
558556

559557
# TODO: only do this for resample, not general groupers...
560558
# this creates a label DataArray since resample doesn't do that somehow
@@ -577,6 +575,18 @@ def _flox_reduce(self, dim, **kwargs):
577575
else:
578576
group = self._unstacked_group
579577

578+
# Do this so we raise the same error message whether flox is present or not.
579+
# Better to control it here than in flox.
580+
if isinstance(group, str):
581+
group = self._original_obj[group]
582+
if dim not in (None, Ellipsis):
583+
if isinstance(dim, str):
584+
dim = (dim,)
585+
if any(
586+
d not in group.dims and d not in self._original_obj.dims for d in dim
587+
):
588+
raise ValueError(f"cannot reduce over dimensions {dim}.")
589+
580590
# TODO: handle bins=N in dask_groupby
581591
if self._bins is not None:
582592
expected_groups = (self._bins,)

xarray/tests/test_groupby.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,13 +514,13 @@ def test_groupby_grouping_errors() -> None:
514514

515515
def test_groupby_reduce_dimension_error(array) -> None:
516516
grouped = array.groupby("y")
517-
with pytest.raises(ValueError, match=r"Cannot reduce over absent dimensions"):
517+
with pytest.raises(ValueError, match=r"cannot reduce over dimensions"):
518518
grouped.mean()
519519

520-
with pytest.raises(ValueError, match=r"Cannot reduce over absent dimensions"):
520+
with pytest.raises(ValueError, match=r"cannot reduce over dimensions"):
521521
grouped.mean("huh")
522522

523-
with pytest.raises(ValueError, match=r"Cannot reduce over absent dimensions"):
523+
with pytest.raises(ValueError, match=r"cannot reduce over dimensions"):
524524
grouped.mean(("x", "y", "asd"))
525525

526526
grouped = array.groupby("y", squeeze=False)

0 commit comments

Comments
 (0)