Skip to content

Use of labels in BinGrouper despite its bins being of type pd.IntervalIndex #10284

Closed
@QuentinMaz

Description

@QuentinMaz

What happened?

Hi,
My code stopped working with the new update. It raises this error when regrouping data of my dataset:

TypeError: Index(...) must be called with a collection of some kind, None was passed

It appears that that the attribute labels is used to create a pd.Index() (link to source code), but this behavior should happen if the bins are not of type pd.IntervalIndex. Sorry if it is unclear (it is my first bug report). See the minimal code provided to reproduce the issue 🙏 .

What did you expect to happen?

The data grouped by "x":

<DatasetGroupBy, grouped over 1 grouper(s), 3 groups in total:
    'x_bins': 3/3 groups present with labels (0,, 1], (1,, 2], (2,, 3]>

Minimal Complete Verifiable Example

import pandas as pd
import xarray as xr
from xarray.groupers import BinGrouper

ds = xr.Dataset(
    {"dummy": ("x", [1, 2, 3, 4, 5])},
    coords={"x": [0.5, 1.5, 2.0, 2.5, 3]}
)

bins = pd.IntervalIndex.from_tuples([(0, 1), (1, 2), (2, 3)])

grouper = BinGrouper(bins=bins)
print(ds.groupby({"x": grouper}))

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?

Maybe there is just a typo in the new code here: I think we should remove not ❓...

Environment

INSTALLED VERSIONS ------------------ commit: None python: 3.12.8 | packaged by Anaconda, Inc. | (main, Dec 11 2024, 16:31:09) [GCC 11.2.0] python-bits: 64 OS: Linux OS-release: 4.4.0-19041-Microsoft machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: C.UTF-8 LOCALE: ('C', 'UTF-8') libhdf5: 1.14.2 libnetcdf: 4.9.4-development

xarray: 2025.4.0
pandas: 2.2.3
numpy: 2.0.2
scipy: 1.14.1
netCDF4: 1.7.2
pydap: None
h5netcdf: None
h5py: None
zarr: 2.18.2
cftime: 1.6.4.post1
nc_time_axis: None
iris: None
bottleneck: None
dask: 2025.2.0
distributed: 2025.2.0
matplotlib: 3.10.0
cartopy: 0.24.1
seaborn: None
numbagg: None
fsspec: 2024.12.0
cupy: None
pint: 0.24.4
sparse: 0.15.4
flox: 0.10.0
numpy_groupies: 0.11.2
setuptools: 75.1.0
pip: 24.2
conda: None
pytest: 8.3.5
mypy: None
IPython: 8.30.0
sphinx: 7.4.7

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions