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 all commits
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
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/shoyer>`_.
- Better error message when using groupby on an empty DataArray (:issue:`3037`).
By `Hasan Ahmad <https://github.com/HasanAhmadQ7>`_.

.. _whats-new.0.12.2:

Expand Down
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("{} 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)
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