Skip to content

groupby on empty DataArray raises StopIteration #3037

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

Closed
lenaqr opened this issue Jun 21, 2019 · 2 comments · Fixed by #3156
Closed

groupby on empty DataArray raises StopIteration #3037

lenaqr opened this issue Jun 21, 2019 · 2 comments · Fixed by #3156

Comments

@lenaqr
Copy link

lenaqr commented Jun 21, 2019

This seems similar to #1764 and #2240 so apologies if it is a duplicate, but I have a minimal example where it happens on an empty DataArray:

Code Sample

import xarray as xr
xr.DataArray([], dims='dim').groupby('dim').mean()  # raises StopIteration

Problem Description

Using groupby on an empty DataArray or Dataset raises StopIteration. It should raise a more meaningful error.

In particular, I had this issue in a function I was calling inside of a generator, so the StopIteration just broke out of the generator and it took some digging to figure out what was going wrong in my code.

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 3.10.0-862.14.4.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.utf8
LOCALE: en_US.UTF-8

xarray: 0.10.7
pandas: 0.23.0
numpy: 1.14.3
scipy: 1.1.0
netCDF4: 1.4.0
h5netcdf: 0.6.1
h5py: 2.7.1
Nio: None
zarr: None
bottleneck: 1.2.1
cyordereddict: None
dask: 0.17.5
distributed: 1.21.8
matplotlib: 2.2.2
cartopy: None
seaborn: 0.8.1
setuptools: 39.1.0
pip: 10.0.1
conda: None
pytest: 3.5.1
IPython: 6.4.0
sphinx: 1.7.4

@shoyer
Copy link
Member

shoyer commented Jun 24, 2019

Agreed, this is definitely a bug. I'm not sure if we can define meaningful behavior for this case, but at the least we should raise a better error.

@HasanAhmadQ7
Copy link
Contributor

HasanAhmadQ7 commented Jul 9, 2019

@shoyer I would like to work on this issue (hopefully my first contribution).

I believe the error should indicate that we cannot groupby an empty group. This would be consistent with the documentation:

DataArray.groupby(group, squeeze: bool = True, restore_coord_dims: Optional[bool] = None)
Parameters
groupstr, DataArray or IndexVariable
Array whose unique values should be used to group this array. If a string, must be the name of a variable contained in this dataset.

For the groupby class, we can raise an error in case the groupby object instantiation does not specify values to be grouped by. In the case above:

ValueError: variable to groupby must not be empty

If you find such a solution acceptable, I would create a pull request.

Thank you in advance

HasanAhmadQ7 pushed a commit to HasanAhmadQ7/xarray that referenced this issue Jul 22, 2019
max-sixty added a commit that referenced this issue Jul 23, 2019
* 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

* BUG: groupby on empty DataArray

Adding the name of the group to the error message. Making the message more user friendly

* DOC: update whats-new.rst

include bug: #3037 fix

* Update doc/whats-new.rst

Co-Authored-By: Maximilian Roos <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants