Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix groupby tests #9716

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2848,20 +2848,27 @@ def test_multiple_groupers(use_flox) -> None:
xy=UniqueGrouper(labels=["a", "b", "c"]),
eagerly_compute_group=eagerly_compute_group,
)
with raise_if_dask_computes(max_computes=1):
if eagerly_compute_group:
with pytest.warns(DeprecationWarning):
gb = b.groupby(**kwargs) # type: ignore[arg-type]
else:
gb = b.groupby(**kwargs) # type: ignore[arg-type]
assert is_chunked_array(gb.encoded.codes.data)
assert not gb.encoded.group_indices
expected = xr.DataArray(
[[[1, 1, 1], [np.nan, 1, 2]]] * 4,
dims=("z", "x", "xy"),
coords={"xy": ("xy", ["a", "b", "c"], {"foo": "bar"})},
)
assert_identical(gb.count(), expected)
if eagerly_compute_group:
with raise_if_dask_computes(max_computes=1):
with pytest.warns(DeprecationWarning):
gb = b.groupby(**kwargs) # type: ignore[arg-type]
assert_identical(gb.count(), expected)
else:
with raise_if_dask_computes(max_computes=0):
gb = b.groupby(**kwargs) # type: ignore[arg-type]
assert is_chunked_array(gb.encoded.codes.data)
assert not gb.encoded.group_indices
if has_flox:
with raise_if_dask_computes(max_computes=1):
assert_identical(gb.count(), expected)
else:
with pytest.raises(ValueError, match="when lazily grouping"):
gb.count()


@pytest.mark.parametrize("use_flox", [True, False])
Expand Down Expand Up @@ -3003,11 +3010,6 @@ def test_lazy_grouping(grouper, expect_index):
pd.testing.assert_index_equal(encoded.full_index, expect_index)
np.testing.assert_array_equal(encoded.unique_coord.values, np.array(expect_index))

lazy = (
xr.Dataset({"foo": data}, coords={"zoo": data})
.groupby(zoo=grouper, eagerly_compute_group=False)
.count()
)
eager = (
xr.Dataset({"foo": data}, coords={"zoo": data.compute()})
.groupby(zoo=grouper)
Expand All @@ -3017,9 +3019,16 @@ def test_lazy_grouping(grouper, expect_index):
{"foo": (encoded.codes.name, np.ones(encoded.full_index.size))},
coords={encoded.codes.name: expect_index},
)
assert_identical(eager, lazy)
assert_identical(eager, expected)

if has_flox:
lazy = (
xr.Dataset({"foo": data}, coords={"zoo": data})
.groupby(zoo=grouper, eagerly_compute_group=False)
.count()
)
assert_identical(eager, lazy)


@requires_dask
def test_lazy_grouping_errors():
Expand Down
Loading