Skip to content

Commit eecf4ae

Browse files
HasanAhmadQ7HasanAhmadQ7
HasanAhmadQ7
authored and
HasanAhmadQ7
committed
BUG: groupby on empty DataArray raises StopIteration
Using groupby on an empty DataArray or Dataset raises StopIteration. It should raise a more meaningful error. Resolves: 3037
1 parent 118f4d9 commit eecf4ae

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

xarray/core/groupby.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ def __init__(self, obj, group, squeeze=False, grouper=None, bins=None,
232232
raise TypeError('`group` must be an xarray.DataArray or the '
233233
'name of an xarray variable or dimension')
234234
group = obj[group]
235+
if len(group) == 0:
236+
raise ValueError("Group must not be empty")
237+
235238
if group.name not in obj.coords and group.name in obj.dims:
236239
# DummyGroups should not appear on groupby results
237240
group = _DummyGroup(obj, group.name, group.coords)

xarray/tests/test_groupby.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ def func(arg1, arg2, arg3=0):
104104
actual = dataset.groupby('x').apply(func, args=(1,), arg3=1)
105105
assert_identical(expected, actual)
106106

107+
def test_da_groupby_empty():
108+
109+
empty_array = xr.DataArray([], dims='dim')
110+
111+
with pytest.raises(ValueError):
112+
empty_array.groupby('dim')
113+
114+
107115

108116
def test_da_groupby_quantile():
109117

0 commit comments

Comments
 (0)