Skip to content

Warn when grouping by dask array. #6788

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
12 changes: 11 additions & 1 deletion xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from .indexes import create_default_index_implicit, filter_indexes_from_coords
from .npcompat import QUANTILE_METHODS, ArrayLike
from .options import _get_keep_attrs
from .pycompat import integer_types
from .pycompat import integer_types, is_duck_dask_array
from .types import T_Xarray
from .utils import (
either_dict_or_kwargs,
Expand Down Expand Up @@ -377,6 +377,16 @@ def __init__(
self._unstacked_group = group
self._bins = bins

if not isinstance(group, _DummyGroup) and is_duck_dask_array(group.data):
warnings.warn(
"Grouping by a dask array computes that array. "
"This will raise an error in the future. "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My naive dream would have been that it successfully grouped the dask array lazily in the future. But that's maybe for the even further future if even possible?

Copy link
Contributor Author

@dcherian dcherian Jul 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 I don't know what I was thinking...

Yes we can now do that with flox...

"Use `.groupby(group.compute())` to avoid an error in the future.",
UserWarning,
stacklevel=3,
)
group = group.compute()

group, obj, stacked_dim, inserted_dims = _ensure_1d(group, obj)
(group_dim,) = group.dims

Expand Down
7 changes: 7 additions & 0 deletions xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,13 @@ def test_groupby_repr_datetime(obj) -> None:
assert actual == expected


@requires_dask
def test_groupby_dask_array_raises_warning():
ds = Dataset({"foo": ("x", np.arange(10)), "baz": ("x", np.arange(10))}).chunk()
with pytest.warns(UserWarning):
ds.groupby("baz")


def test_groupby_drops_nans() -> None:
# GH2383
# nan in 2D data variable (requires stacking)
Expand Down