You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When grouping by a dimension with no duplicate coordinates, reduction operations fail. Reductions don't fail if there are duplicate coordinates, or if an empty tuple is provided in the reduction operation.
What did you expect to happen?
Reductions happening after a groupby operation which leads to each group being a singleton should not fail. By default it should mimic the same behaviour as when an empty tuple is passed to the reduction operation.
Minimal Complete Verifiable Example
importnumpyasnpimportxarrayasxra=xr.DataArray(range(3), coords={'a': [0, 1, 1]})
b=xr.DataArray(range(3), coords={'a': [0, 1, 2]})
# Works completely finea.groupby('a').sum()
# What is expected from the below:b.groupby('a').sum(tuple())
# Raises value errorb.groupby('a').sum()
MVCE confirmation
Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
Complete example — the example is self-contained, including all data and the text of any traceback.
Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
New issue — a search of GitHub Issues suggests this is not a duplicate.
Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output
ValueErrorTraceback (mostrecentcalllast)
File/private/tmp/foo.py:11, in<module>8a.groupby('a').sum()
10# Raises value error--->11b.groupby('a').sum()
13# What is expected from the above:14b.groupby('a').sum(tuple())
File~/miniconda3/lib/python3.10/site-packages/xarray/core/_aggregations.py:6204, inDataArrayGroupByAggregations.sum(self, dim, skipna, min_count, keep_attrs, **kwargs)
6194returnself._flox_reduce(
6195func="sum",
6196dim=dim,
(...)
6201**kwargs,
6202 )
6203else:
->6204returnself.reduce(
6205duck_array_ops.sum,
6206dim=dim,
6207skipna=skipna,
6208min_count=min_count,
6209keep_attrs=keep_attrs,
6210**kwargs,
6211 )
File~/miniconda3/lib/python3.10/site-packages/xarray/core/groupby.py:1478, inDataArrayGroupByBase.reduce(self, func, dim, axis, keep_attrs, keepdims, shortcut, **kwargs)
1468defreduce_array(ar: DataArray) ->DataArray:
1469returnar.reduce(
1470func=func,
1471dim=dim,
(...)
1475**kwargs,
1476 )
->1478check_reduce_dims(dim, self.dims)
1480returnself.map(reduce_array, shortcut=shortcut)
File~/miniconda3/lib/python3.10/site-packages/xarray/core/groupby.py:69, incheck_reduce_dims(reduce_dims, dimensions)
67reduce_dims= [reduce_dims]
68ifany(dimnotindimensionsfordiminreduce_dims):
--->69raiseValueError(
70f"cannot reduce over dimensions {reduce_dims!r}. expected either '...' "71f"to reduce over all dimensions or one or more of {dimensions!r}."72 )
ValueError: cannotreduceoverdimensions ['a']. expectedeither'...'toreduceoveralldimensionsoroneormoreof ().
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: None
python: 3.10.5 | packaged by conda-forge | (main, Jun 14 2022, 07:03:09) [Clang 13.0.1 ]
python-bits: 64
OS: Darwin
OS-release: 23.0.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: ('en_GB', 'UTF-8')
libhdf5: 1.12.1
libnetcdf: None
Thanks for opening your first issue here at xarray! Be sure to follow the issue template!
If you have an idea for a solution, we would really welcome a Pull Request with proposed changes.
See the Contributing Guide for more.
It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better.
Thank you!
IIUC, we do want to raise an error on foo.groupby('bar').sum(). We're trying to be more explicit on the dimension argument, such that foo.groupby('bar').sum(...) is required to group over all dimensions.
IIUC the size of the groups shouldn't affect what raises an error
Possibly others have more insight here — are my claims on the intended behavior at least correct @pydata/xarray ?
The error message is quite bad — expected either '...' to reduce over all dimensions or one or more of ().. The of (). comes from there being no dimensions, it's not trying to suggest using ().
What happened?
When grouping by a dimension with no duplicate coordinates, reduction operations fail. Reductions don't fail if there are duplicate coordinates, or if an empty tuple is provided in the reduction operation.
What did you expect to happen?
Reductions happening after a groupby operation which leads to each group being a singleton should not fail. By default it should mimic the same behaviour as when an empty tuple is passed to the reduction operation.
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: None
python: 3.10.5 | packaged by conda-forge | (main, Jun 14 2022, 07:03:09) [Clang 13.0.1 ]
python-bits: 64
OS: Darwin
OS-release: 23.0.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: ('en_GB', 'UTF-8')
libhdf5: 1.12.1
libnetcdf: None
xarray: 2023.6.0
pandas: 1.5.3
numpy: 1.23.5
scipy: 1.8.1
netCDF4: None
pydap: None
h5netcdf: None
h5py: 3.6.0
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
iris: None
bottleneck: 1.3.5
dask: 2022.8.0
distributed: 2022.8.0
matplotlib: 3.5.2
cartopy: None
seaborn: None
numbagg: None
fsspec: 2023.4.0
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 67.8.0
pip: 21.3.1
conda: 4.13.0
pytest: None
mypy: None
IPython: 8.4.0
sphinx: None
The text was updated successfully, but these errors were encountered: