Skip to content

Commit 39e58b2

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 39e58b2

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
@@ -105,6 +105,14 @@ def func(arg1, arg2, arg3=0):
105105
assert_identical(expected, actual)
106106

107107

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

110118
array = xr.DataArray([1, 2, 3, 4, 5, 6],

0 commit comments

Comments
 (0)