Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ Bug Fixes
- Fix a major performance regression in :py:meth:`Coordinates.to_index` (and
consequently :py:meth:`Dataset.to_dataframe`) caused by converting the cached
code ndarrays into Python lists (:issue:`11305`).
- Fix :py:meth:`DataArray.idxmax` and :py:meth:`DataArray.idxmin` for interval
coordinates by preserving pandas extension index adapters during label lookup
(:issue:`11300`).


Documentation
Expand Down
3 changes: 3 additions & 0 deletions xarray/compat/array_api_compat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np

from xarray.core.utils import is_allowed_extension_array
from xarray.namedarray.pycompat import array_type


Expand Down Expand Up @@ -78,5 +79,7 @@ def to_like_array(array, like):
xp = get_array_namespace(like)
if xp is not np:
return xp.asarray(array)
if is_allowed_extension_array(array):
return np.asarray(array)
# avoid casting things like pint quantities to numpy arrays
return array
24 changes: 24 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -5530,6 +5530,30 @@ def test_idxmax(
result7 = ar0.idxmax(fill_value=-1j)
assert_identical(result7, expected7)

@pytest.mark.parametrize(
("func_name", "values"),
[
pytest.param("idxmax", [False, True, True], id="idxmax"),
pytest.param("idxmin", [True, False, True], id="idxmin"),
],
)
def test_idxminmax_interval_coords(
self,
x: np.ndarray,
minindex: int | float,
maxindex: int | float,
nanindex: int | None,
func_name: Literal["idxmax", "idxmin"],
values: list[bool],
) -> None:
interval_index = pd.IntervalIndex.from_breaks([0, 1, 2, 3])
array = xr.DataArray(values, dims=["z"], coords={"z": interval_index})

result = getattr(array, func_name)()

expected = xr.DataArray(pd.Interval(1, 2, closed="right"), name="z")
assert_identical(result, expected)

@pytest.mark.filterwarnings(
"ignore:Behaviour of argmin/argmax with neither dim nor :FutureWarning"
)
Expand Down
Loading