Skip to content

Fix performance regression in interp from #9881 #10370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 30, 2025
Merged
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
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ Performance
in :py:class:`~xarray.indexing.VectorizedIndexer` and :py:class:`~xarray.indexing.OuterIndexer`
(:issue:`10316`).
By `Jesse Rusak <https://github.com/jder>`_.
- Fix performance regression in interp where more data was loaded than was necessary. (:issue:`10287`).
By `Deepak Cherian <https://github.com/dcherian>`_.
- Speed up encoding of :py:class:`cftime.datetime` objects by roughly a factor
of three (:pull:`8324`). By `Antoine Gibek <https://github.com/antscloud>`_.

Expand Down
13 changes: 7 additions & 6 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3804,15 +3804,16 @@ def _validate_interp_indexer(x, new_x):
for k, v in indexers.items()
}

# optimization: subset to coordinate range of the target index
if method in ["linear", "nearest"]:
for k, v in validated_indexers.items():
obj, newidx = missing._localize(obj, {k: v})
validated_indexers[k] = newidx[k]

has_chunked_array = bool(
any(is_chunked_array(v._data) for v in obj._variables.values())
)
if has_chunked_array:
# optimization: subset to coordinate range of the target index
if method in ["linear", "nearest"]:
for k, v in validated_indexers.items():
obj, newidx = missing._localize(obj, {k: v})
validated_indexers[k] = newidx[k]
# optimization: create dask coordinate arrays once per Dataset
# rather than once per Variable when dask.array.unify_chunks is called later
# GH4739
Expand All @@ -3828,7 +3829,7 @@ def _validate_interp_indexer(x, new_x):
continue

use_indexers = (
dask_indexers if is_duck_dask_array(var.data) else validated_indexers
dask_indexers if is_duck_dask_array(var._data) else validated_indexers
)

dtype_kind = var.dtype.kind
Expand Down
28 changes: 28 additions & 0 deletions xarray/tests/test_missing.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from __future__ import annotations

import itertools
from unittest import mock

import numpy as np
import pandas as pd
import pytest

import xarray as xr
from xarray.core import indexing
from xarray.core.missing import (
NumpyInterpolator,
ScipyInterpolator,
Expand Down Expand Up @@ -772,3 +774,29 @@ def test_interpolators_complex_out_of_bounds():
f = interpolator(xi, yi, method=method)
actual = f(x)
assert_array_equal(actual, expected)


@requires_scipy
def test_indexing_localize():
# regression test for GH10287
ds = xr.Dataset(
{
"sigma_a": xr.DataArray(
data=np.ones((16, 8, 36811)),
dims=["p", "t", "w"],
coords={"w": np.linspace(0, 30000, 36811)},
)
}
)

original_func = indexing.NumpyIndexingAdapter.__getitem__

def wrapper(self, indexer):
return original_func(self, indexer)

with mock.patch.object(
indexing.NumpyIndexingAdapter, "__getitem__", side_effect=wrapper, autospec=True
) as mock_func:
ds["sigma_a"].interp(w=15000.5)
actual_indexer = mock_func.mock_calls[0].args[1]._key
assert actual_indexer == (slice(None), slice(None), slice(18404, 18408))
Loading