Skip to content

Fix interp bug when indexer shares coordinates with array #3758

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 8 commits into from
Mar 13, 2020
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
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ New Features

Bug fixes
~~~~~~~~~
- Fix :py:meth:`Dataset.interp` when indexing array shares coordinates with the
indexed variable (:issue:`3252`).
By `David Huard <https://github.com/huard>`_.

- Fix :py:meth:`Dataset.swap_dims` and :py:meth:`DataArray.swap_dims` producing
index with name reflecting the previous dimension name instead of the new one
Expand Down
11 changes: 11 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2571,6 +2571,17 @@ def interp(
coords = either_dict_or_kwargs(coords, coords_kwargs, "interp")
indexers = dict(self._validate_interp_indexers(coords))

if coords:
# This avoids broadcasting over coordinates that are both in
# the original array AND in the indexing array. It essentially
# forces interpolation along the shared coordinates.
sdims = (
set(self.dims)
.intersection(*[set(nx.dims) for nx in indexers.values()])
.difference(coords.keys())
)
indexers.update({d: self.variables[d] for d in sdims})

obj = self if assume_sorted else self.sortby([k for k in coords])

def maybe_variable(obj, k):
Expand Down
30 changes: 30 additions & 0 deletions xarray/tests/test_interp.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,36 @@ def test_interpolate_nd(case):
assert_allclose(actual.transpose("y", "z"), expected)


@requires_scipy
def test_interpolate_nd_nd():
"""Interpolate nd array with an nd indexer sharing coordinates."""
# Create original array
a = [0, 2]
x = [0, 1, 2]
da = xr.DataArray(
np.arange(6).reshape(2, 3), dims=("a", "x"), coords={"a": a, "x": x}
)

# Create indexer into `a` with dimensions (y, x)
y = [10]
c = {"x": x, "y": y}
ia = xr.DataArray([[1, 2, 2]], dims=("y", "x"), coords=c)
out = da.interp(a=ia)
expected = xr.DataArray([[1.5, 4, 5]], dims=("y", "x"), coords=c)
xr.testing.assert_allclose(out.drop_vars("a"), expected)

# If the *shared* indexing coordinates do not match, interp should fail.
with pytest.raises(ValueError):
c = {"x": [1], "y": y}
ia = xr.DataArray([[1]], dims=("y", "x"), coords=c)
da.interp(a=ia)

with pytest.raises(ValueError):
c = {"x": [5, 6, 7], "y": y}
ia = xr.DataArray([[1]], dims=("y", "x"), coords=c)
da.interp(a=ia)


@pytest.mark.parametrize("method", ["linear"])
@pytest.mark.parametrize("case", [0, 1])
def test_interpolate_scalar(method, case):
Expand Down