Skip to content

groupby on multi-index level is grouping by the multi-index (reappearance of #6836) #9388

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
5 tasks done
carynbear opened this issue Aug 20, 2024 · 3 comments
Closed
5 tasks done
Labels
bug needs triage Issue that has not been reviewed by xarray team member

Comments

@carynbear
Copy link

What happened?

This is the same issue as #6836

Given a multiindex "x" --> ("one", "two")
Groupby on a multi-index level ("one") is grouping by the multi-index ("x") instead of level.

midx = pd.MultiIndex.from_product([list("abc"), [0, 1]], names=("one", "two"))
mda = xr.DataArray(np.random.rand(6, 3), [("x", midx), ("y", range(3))])
mda.groupby("one").groups

{('a', 0): [0],
 ('a', 1): [1],
 ('b', 0): [2],
 ('b', 1): [3],
 ('c', 0): [4],
 ('c', 1): [5]}

What did you expect to happen?

Should return

{'a': [0, 1],
 'b': [2, 3],
 'c': [4, 5]}

Minimal Complete Verifiable Example

midx = pd.MultiIndex.from_product([list("abc"), [0, 1]], names=("one", "two"))
mda = xr.DataArray(np.random.rand(6, 3), [("x", midx), ("y", range(3))])
mda.groupby("one").groups

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

No response

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS

commit: None
python: 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110]
python-bits: 64
OS: Linux
OS-release: 5.10.0-30-cloud-amd64
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: C.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: None
libnetcdf: None

xarray: 2023.1.0
pandas: 2.0.3
numpy: 1.24.4
scipy: 1.9.3
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.7.5
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: None
pip: None
conda: None
pytest: None
mypy: None
IPython: 8.12.3
sphinx: None

@carynbear carynbear added bug needs triage Issue that has not been reviewed by xarray team member labels Aug 20, 2024
Copy link

welcome bot commented Aug 20, 2024

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!

@carynbear carynbear changed the title groupby (multi-index level) not working correctly on a multi-indexed DataArray or DataSet reappeared: groupby (multi-index level) not working correctly on a multi-indexed DataArray or DataSet Aug 20, 2024
@carynbear carynbear changed the title reappeared: groupby (multi-index level) not working correctly on a multi-indexed DataArray or DataSet reappeared: groupby on multiindex level not working correctly Aug 20, 2024
@carynbear carynbear changed the title reappeared: groupby on multiindex level not working correctly reappeared: groupby on multi-index level is grouping by the multi-index Aug 20, 2024
@carynbear carynbear changed the title reappeared: groupby on multi-index level is grouping by the multi-index groupby on multi-index level is grouping by the multi-index (reappearance of #6836) Aug 20, 2024
@dcherian
Copy link
Contributor

xarray: 2023.1.0

please update xarray

@carynbear
Copy link
Author

Okay it works with 2024.7.0. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs triage Issue that has not been reviewed by xarray team member
Projects
None yet
Development

No branches or pull requests

2 participants