Skip to content

Data*.groupby with sequences yields scalars for len-1 sequences #10246

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

Open
5 tasks done
zerothi opened this issue Apr 24, 2025 · 2 comments · May be fixed by #10271
Open
5 tasks done

Data*.groupby with sequences yields scalars for len-1 sequences #10246

zerothi opened this issue Apr 24, 2025 · 2 comments · May be fixed by #10271

Comments

@zerothi
Copy link
Contributor

zerothi commented Apr 24, 2025

What happened?

When executing the following code:

import xarray as xr
import numpy as np

mda = xr.DataArray(np.random.rand(6, 3), [("x", list("abcabc")), ("y", range(3))])
print(mda)

print("scalar groupby")
for value, group in mda.groupby("x"):
    print(value)
    assert isinstance(value, str)
    break

print("multiple groupby")
for value, group in mda.groupby(["x", "y"]):
    print(value)
    assert isinstance(value, tuple)
    break

print("multiple *scalar* groupby")
for value, group in mda.groupby(["x"]):
    print(value)
    assert isinstance(value, tuple)
    break

I would have expected the last invocation to yield a value of a tuple of length 1. When doing variable groupby segments with sequences of varying length it seems important to keep the dimensions of the value returned.

What did you expect to happen?

I would have expected the last value (in multiple scalar groupby) to yield a (value,) instead.

Minimal Complete Verifiable Example

import xarray as xr
import numpy as np

mda = xr.DataArray(np.random.rand(6, 3), [("x", list("abcabc")), ("y", range(3))])
print(mda)

for value, group in mda.groupby(["x"]):
    assert isinstance(value, tuple)

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

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS

commit: None
python: 3.11.7 (main, Feb 13 2024, 19:40:15) [GCC 12.3.0]
python-bits: 64
OS: Linux
OS-release: 6.1.0-33-amd64
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.14.0
libnetcdf: 4.9.2

xarray: 2025.3.1
pandas: 2.0.1
numpy: 1.24.4
scipy: 1.10.1
netCDF4: 1.6.3
pydap: None
h5netcdf: None
h5py: None
zarr: None
cftime: 1.6.2
nc_time_axis: None
iris: None
bottleneck: 1.3.7
dask: 2023.3.2
distributed: None
matplotlib: 3.7.1
cartopy: None
seaborn: 0.13.2
numbagg: None
fsspec: None
cupy: None
pint: 0.23
sparse: None
flox: None
numpy_groupies: None
setuptools: 69.1.0
pip: 25.0.1
conda: None
pytest: 8.3.2
mypy: None
IPython: 8.21.0
sphinx: 7.2.6
None

@zerothi zerothi added bug needs triage Issue that has not been reviewed by xarray team member labels Apr 24, 2025
@dcherian dcherian added topic-groupby and removed needs triage Issue that has not been reviewed by xarray team member labels Apr 26, 2025
@dcherian
Copy link
Contributor

Yes I agree, this was an oversight. Are you able to contribute a fix?

@zerothi zerothi linked a pull request Apr 29, 2025 that will close this issue
4 tasks
@zerothi
Copy link
Contributor Author

zerothi commented Apr 29, 2025

Yes I agree, this was an oversight. Are you able to contribute a fix?

Done, lets see if ci has anything...

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.

2 participants