Skip to content

Fix idxminmax for interval coordinates#11313

Open
lavaFreak wants to merge 2 commits intopydata:mainfrom
lavaFreak:interval-idxmax-fix
Open

Fix idxminmax for interval coordinates#11313
lavaFreak wants to merge 2 commits intopydata:mainfrom
lavaFreak:interval-idxmax-fix

Conversation

@lavaFreak
Copy link
Copy Markdown

Description

Fix DataArray.idxmax() and DataArray.idxmin() when the reduced dimension uses interval coordinates. _calc_idxminmax() was replacing the coordinate's original pandas index adapter with a raw extension array before the final label lookup, which causes scalar interval selections to fail with TypeError: len() of unsized object.

This keeps pandas extension-backed coordinates on their original adapter for the final lookup, adds a regression test for both idxmax and idxmin, and documents the bug fix in doc/whats-new.rst.

Checklist

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
      Tools: Codex

# ExtensionArray scalar wrapper.
pass
else:
coord = coord.copy(data=to_like_array(array[dim].data, array.data))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i suspect the real solution is to fix to_like_array to handle extension arrays?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I moved the fix into to_like_array() in d0f22b94 instead of keeping the one-off special case in _calc_idxminmax().

For NumPy-backed lookups, to_like_array() now converts allowed pandas extension arrays to a NumPy object array, which fixes the scalar interval selection path while keeping the idxmin/idxmax regression green.

Validation I reran locally:

  • .venv/bin/python -m pytest -o addopts= xarray/tests/test_dataarray.py -k 'test_idxminmax_interval_coords or test_idxmax or test_idxmin'
  • .venv/bin/python -m ruff check xarray/computation/computation.py xarray/compat/array_api_compat.py xarray/tests/test_dataarray.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.idxmax() fails if coordinates are intervals

2 participants