diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 6a08246182c..3885b162bd0 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -45,6 +45,9 @@ Bug fixes By `Bruce Merry `_. - Fix unintended load on datasets when calling :py:meth:`DataArray.plot.scatter` (:pull:`9818`). By `Jimmy Westling `_. +- Fix interpolation when non-numeric coordinate variables are present (:issue:`8099`, :issue:`9839`). + By `Deepak Cherian `_. + Documentation ~~~~~~~~~~~~~ diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index ea17a69f827..dd63e52c436 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -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, diff --git a/xarray/tests/test_interp.py b/xarray/tests/test_interp.py index d602cb96a6a..e2561515d65 100644 --- a/xarray/tests/test_interp.py +++ b/xarray/tests/test_interp.py @@ -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") + + # with numeric only + expected = ds[["x"]].interp(a=t, method="linear") + assert_identical(actual[["x"]], expected)