Skip to content

Commit

Permalink
Fix interpolation when non-numeric coords are present.
Browse files Browse the repository at this point in the history
Closes #8099
Closes #9839
  • Loading branch information
dcherian committed Dec 13, 2024
1 parent 49502fc commit 766b6b1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ Bug fixes
By `Bruce Merry <https://github.com/bmerry>`_.
- Fix unintended load on datasets when calling :py:meth:`DataArray.plot.scatter` (:pull:`9818`).
By `Jimmy Westling <https://github.com/illviljan>`_.
- Fix interpolation when non-numeric coordinate variables are present (:issue:`8099`, :issue:`9839`).
By `Deepak Cherian <https://github.com/dcherian>`_.


Documentation
~~~~~~~~~~~~~
Expand Down
5 changes: 3 additions & 2 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4214,10 +4214,11 @@ def _validate_interp_indexer(x, new_x):
# interpolated along:
variables[name] = var

if reindex:
reindex_indexers = {
if reindex and (
reindex_indexers := {
k: v for k, (_, v) in validated_indexers.items() if v.dims == (k,)
}
):
reindexed = alignment.reindex(
obj,
indexers=reindex_indexers,
Expand Down
20 changes: 20 additions & 0 deletions xarray/tests/test_interp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,3 +1055,23 @@ def test_interp1d_complex_out_of_bounds() -> None:
expected = da.interp(time=3.5, kwargs=dict(fill_value=np.nan + np.nan * 1j))
actual = da.interp(time=3.5)
assert_identical(actual, expected)


def test_interp_non_numeric():
# regression test for GH8099, GH9839
ds = xr.Dataset({"x": ("a", np.arange(4))}, coords={"a": (np.arange(4) - 1.5)})

t = xr.DataArray(
np.random.randn(6).reshape((2, 3)) * 0.5,
dims=["r", "s"],
coords={"r": np.arange(2) - 0.5, "s": np.arange(3) - 1},
)

ds["m"] = ds.x > 1

# different dimensions for `x`, compare
actual = ds.interp(a=t, method="linear")

Check failure on line 1073 in xarray/tests/test_interp.py

View workflow job for this annotation

GitHub Actions / ubuntu-latest py3.10 bare-minimum

test_interp_non_numeric ModuleNotFoundError: No module named 'scipy'

# with numeric only
expected = ds[["x"]].interp(a=t, method="linear")
assert_identical(actual[["x"]], expected)

0 comments on commit 766b6b1

Please sign in to comment.