`.idxmax()` fails if coordinates are intervals
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
What happened?
<operation including groupby_bins>.idxmax() raised TypeError: len() of unsized object
What did you expect to happen?
It should return the index of the greatest value.
Minimal Complete Verifiable Example
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "pandas",
# "xarray[complete]@git+https://github.com/pydata/xarray.git@main",
# ]
# ///
#
# This script automatically imports the development branch of xarray to check for issues.
# Please delete this header if you have _not_ tested this script with `uv run`!
import xarray as xr
xr.show_versions()
import pandas as pd
idx = pd.IntervalIndex.from_breaks([0, 1, 2, 3])
da = xr.DataArray([False, True, True], dims=["z"], coords={"z": idx})
print(da.idxmax())
Steps to reproduce
Run the above uv-backed script. Note that this fails for pandas 2.x and 3.x.
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
Traceback (most recent call last):
File "xarray/idxmax_issue_reproducer.py", line 23, in <module>
print(da.idxmax())
^^^^^^^^^^^
File "xarray/xarray/core/dataarray.py", line 6194, in idxmax
return computation._calc_idxminmax(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "xarray/xarray/computation/computation.py", line 1014, in _calc_idxminmax
res = index._replace(coord[(index.variable,)]).rename(dim)
~~~~~^^^^^^^^^^^^^^^^^^^
File "xarray/xarray/core/variable.py", line 835, in __getitem__
return self._finalize_indexing_result(dims, data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "xarray/xarray/core/variable.py", line 839, in _finalize_indexing_result
return self._replace(dims=dims, data=data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "xarray/xarray/core/variable.py", line 991, in _replace
return type(self)(dims, data, attrs, encoding, fastpath=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "xarray/xarray/core/variable.py", line 398, in __init__
super().__init__(
File "xarray/xarray/namedarray/core.py", line 261, in __init__
self._dims = self._parse_dimensions(dims)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "xarray/xarray/namedarray/core.py", line 504, in _parse_dimensions
if len(dims) != self.ndim:
^^^^^^^^^
File "xarray/xarray/namedarray/core.py", line 413, in ndim
return len(self.shape)
^^^^^^^^^^
File "xarray/xarray/namedarray/core.py", line 460, in shape
return self._data.shape
^^^^^^^^^^^^^^^^
File "xarray/xarray/core/utils.py", line 705, in shape
return self.array.shape
^^^^^^^^^^^^^^^^
File ".cache/uv/environments-v2/idxmax-issue-reproducer-bea2356faa9cc87a/lib/python3.11/site-packages/pandas/core/arrays/base.py", line 743, in shape
return (len(self),)
^^^^^^^^^
File ".cache/uv/environments-v2/idxmax-issue-reproducer-bea2356faa9cc87a/lib/python3.11/site-packages/pandas/core/arrays/interval.py", line 823, in __len__
return len(self._left)
^^^^^^^^^^^^^^^
TypeError: len() of unsized object
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: f7e47a19726321e56d74bca896eb55c6f330506b
python: 3.11.0rc1 (main, Mar 3 2026, 12:05:23) [GCC 11.4.0]
python-bits: 64
OS: Linux
OS-release: 5.15.0-174-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.14.6
libnetcdf: 4.9.3
xarray: 10000.dev6419+gf7e47a197
pandas: 3.0.2
numpy: 2.4.4
scipy: 1.17.1
netCDF4: 1.7.4
pydap: 3.5.9
h5netcdf: 1.8.1
h5py: 3.16.0
zarr: 3.1.6
cftime: 1.6.5
nc_time_axis: 1.4.1
iris: None
bottleneck: 1.6.0
dask: 2026.3.0
distributed: 2026.3.0
matplotlib: 3.10.8
cartopy: 0.25.0
seaborn: 0.13.2
numbagg: 0.9.4
fsspec: 2026.3.0
cupy: None
pint: None
sparse: 0.18.0
flox: 0.11.2
numpy_groupies: 0.11.3
setuptools: None
pip: None
conda: None
pytest: None
mypy: None
IPython: None
sphinx: None
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with xarray/core/computation/computation.py and _calc_idxminmax, then follow the indexing path into xarray/core/variable.py shown in the traceback. Run the uv-backed reproducer with the IntervalIndex and inspect how the coordinate is selected. Done means idxmax() returns the index of the greatest value for interval coordinates without raising TypeError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- pandas, python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100