Skip to content

Commit b78f853

Browse files
committed
Fix groupby tests
Closes pydata#9715
1 parent af7c6d4 commit b78f853

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

xarray/tests/test_groupby.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,20 +2848,25 @@ def test_multiple_groupers(use_flox) -> None:
28482848
xy=UniqueGrouper(labels=["a", "b", "c"]),
28492849
eagerly_compute_group=eagerly_compute_group,
28502850
)
2851+
expected = xr.DataArray(
2852+
[[[1, 1, 1], [np.nan, 1, 2]]] * 4,
2853+
dims=("z", "x", "xy"),
2854+
coords={"xy": ("xy", ["a", "b", "c"], {"foo": "bar"})},
2855+
)
28512856
with raise_if_dask_computes(max_computes=1):
28522857
if eagerly_compute_group:
28532858
with pytest.warns(DeprecationWarning):
28542859
gb = b.groupby(**kwargs) # type: ignore[arg-type]
2860+
assert_identical(gb.count(), expected)
28552861
else:
28562862
gb = b.groupby(**kwargs) # type: ignore[arg-type]
28572863
assert is_chunked_array(gb.encoded.codes.data)
28582864
assert not gb.encoded.group_indices
2859-
expected = xr.DataArray(
2860-
[[[1, 1, 1], [np.nan, 1, 2]]] * 4,
2861-
dims=("z", "x", "xy"),
2862-
coords={"xy": ("xy", ["a", "b", "c"], {"foo": "bar"})},
2863-
)
2864-
assert_identical(gb.count(), expected)
2865+
if has_flox:
2866+
assert_identical(gb.count(), expected)
2867+
else:
2868+
with pytest.raises(ValueError, match="when lazily grouping"):
2869+
gb.count()
28652870

28662871

28672872
@pytest.mark.parametrize("use_flox", [True, False])
@@ -3003,11 +3008,6 @@ def test_lazy_grouping(grouper, expect_index):
30033008
pd.testing.assert_index_equal(encoded.full_index, expect_index)
30043009
np.testing.assert_array_equal(encoded.unique_coord.values, np.array(expect_index))
30053010

3006-
lazy = (
3007-
xr.Dataset({"foo": data}, coords={"zoo": data})
3008-
.groupby(zoo=grouper, eagerly_compute_group=False)
3009-
.count()
3010-
)
30113011
eager = (
30123012
xr.Dataset({"foo": data}, coords={"zoo": data.compute()})
30133013
.groupby(zoo=grouper)
@@ -3017,9 +3017,16 @@ def test_lazy_grouping(grouper, expect_index):
30173017
{"foo": (encoded.codes.name, np.ones(encoded.full_index.size))},
30183018
coords={encoded.codes.name: expect_index},
30193019
)
3020-
assert_identical(eager, lazy)
30213020
assert_identical(eager, expected)
30223021

3022+
if has_flox:
3023+
lazy = (
3024+
xr.Dataset({"foo": data}, coords={"zoo": data})
3025+
.groupby(zoo=grouper, eagerly_compute_group=False)
3026+
.count()
3027+
)
3028+
assert_identical(eager, lazy)
3029+
30233030

30243031
@requires_dask
30253032
def test_lazy_grouping_errors():

0 commit comments

Comments
 (0)