Skip to content

BUG: fix+test groupby on empty DataArray raises StopIteration #3156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("Group must not be empty")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add the name of the group to the error message?


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)
Expand Down
8 changes: 8 additions & 0 deletions xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down