Skip to content

.idxmax() fails if coordinates are intervals #11300

@j-haacker

Description

@j-haacker

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

Details

INSTALLED VERSIONS

commit: f7e47a1
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugneeds triageIssue that has not been reviewed by xarray team member

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions