diff --git a/xarray/core/groupby.py b/xarray/core/groupby.py index 5fa78ae76de..768246c2529 100644 --- a/xarray/core/groupby.py +++ b/xarray/core/groupby.py @@ -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, @@ -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. " + "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 diff --git a/xarray/tests/test_groupby.py b/xarray/tests/test_groupby.py index a006e54468a..aadebd7e5c3 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -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)