diff --git a/doc/whats-new.rst b/doc/whats-new.rst index a8c5342374e..b0b2bfefb5f 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -83,6 +83,8 @@ Bug fixes - Fix HDF5 error that could arise when reading multiple groups from a file at once (:issue:`2954`). By `Stephan Hoyer `_. +- Better error message when using groupby on an empty DataArray (:issue:`3037`). + By `Hasan Ahmad `_. .. _whats-new.0.12.2: diff --git a/xarray/core/groupby.py b/xarray/core/groupby.py index eb3b850c6c3..0649ecab44f 100644 --- a/xarray/core/groupby.py +++ b/xarray/core/groupby.py @@ -232,6 +232,9 @@ def __init__(self, obj, group, squeeze=False, grouper=None, bins=None, raise TypeError('`group` must be an xarray.DataArray or the ' 'name of an xarray variable or dimension') group = obj[group] + if len(group) == 0: + raise ValueError("{} must not be empty".format(group.name)) + if group.name not in obj.coords and group.name in obj.dims: # DummyGroups should not appear on groupby results group = _DummyGroup(obj, group.name, group.coords) diff --git a/xarray/tests/test_groupby.py b/xarray/tests/test_groupby.py index 5433bd00f9d..0f1adf3d45f 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -105,6 +105,14 @@ def func(arg1, arg2, arg3=0): assert_identical(expected, actual) +def test_da_groupby_empty(): + + empty_array = xr.DataArray([], dims='dim') + + with pytest.raises(ValueError): + empty_array.groupby('dim') + + def test_da_groupby_quantile(): array = xr.DataArray([1, 2, 3, 4, 5, 6],