From bdce0a8207f5ff410e21635ca31d963eb7d87d30 Mon Sep 17 00:00:00 2001 From: dcherian Date: Fri, 15 Jul 2022 09:09:28 -0600 Subject: [PATCH 1/2] Warn when grouping by dask array. --- xarray/core/groupby.py | 12 +++++++++++- xarray/tests/test_groupby.py | 7 +++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/xarray/core/groupby.py b/xarray/core/groupby.py index 5fa78ae76de..c4540f46be5 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 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) From d752672f83abad17ba37a4b13a101761b0b9fa41 Mon Sep 17 00:00:00 2001 From: dcherian Date: Fri, 15 Jul 2022 11:11:29 -0600 Subject: [PATCH 2/2] FIx mypy? --- xarray/core/groupby.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/groupby.py b/xarray/core/groupby.py index c4540f46be5..768246c2529 100644 --- a/xarray/core/groupby.py +++ b/xarray/core/groupby.py @@ -377,7 +377,7 @@ def __init__( self._unstacked_group = group self._bins = bins - if is_duck_dask_array(group.data): + 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. "