Skip to content

Do not transpose 1d arrays during interpolation #5542

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
50 changes: 30 additions & 20 deletions xarray/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,33 +624,43 @@ def interp(var, indexes_coords, method, **kwargs):
kwargs["bounds_error"] = kwargs.get("bounds_error", False)

result = var
# decompose the interpolation into a succession of independant interpolation
for indexes_coords in decompose_interp(indexes_coords):
var = result

if len(var.dims) < 2:
# target dimensions
dims = list(indexes_coords)
x, new_x = zip(*[indexes_coords[d] for d in dims])
destination = broadcast_variables(*new_x)

# transpose to make the interpolated axis to the last position
broadcast_dims = [d for d in var.dims if d not in dims]
original_dims = broadcast_dims + dims
new_dims = broadcast_dims + list(destination[0].dims)
interped = interp_func(
var.transpose(*original_dims).data, x, destination, method, kwargs
)
interped = interp_func(var.data, x, destination, method, kwargs)
result = Variable(destination[0].dims, interped, attrs=var.attrs)

result = Variable(new_dims, interped, attrs=var.attrs)
else:
# decompose the interpolation into a succession of independant interpolation
for indexes_coords in decompose_interp(indexes_coords):
var = result

# target dimensions
dims = list(indexes_coords)
x, new_x = zip(*[indexes_coords[d] for d in dims])
destination = broadcast_variables(*new_x)

# transpose to make the interpolated axis to the last position
broadcast_dims = [d for d in var.dims if d not in dims]
original_dims = broadcast_dims + dims
interped = interp_func(
var.transpose(*original_dims).data, x, destination, method, kwargs
)

# dimension of the output array
out_dims = OrderedSet()
for d in var.dims:
if d in dims:
out_dims.update(indexes_coords[d][1].dims)
else:
out_dims.add(d)
result = result.transpose(*out_dims)
new_dims = broadcast_dims + list(destination[0].dims)
result = Variable(new_dims, interped, attrs=var.attrs)

# dimension of the output array
out_dims = OrderedSet()
for d in var.dims:
if d in dims:
out_dims.update(indexes_coords[d][1].dims)
else:
out_dims.add(d)
result = result.transpose(*out_dims)
return result


Expand Down