From a664a9e2d1f991ce18f656450837c1f8eba77ea9 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sun, 27 Jun 2021 22:54:46 +0200 Subject: [PATCH 1/2] Do not transpose 1d arrays --- xarray/core/missing.py | 50 +++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/xarray/core/missing.py b/xarray/core/missing.py index c576e0718c6..ca8d35cb938 100644 --- a/xarray/core/missing.py +++ b/xarray/core/missing.py @@ -617,33 +617,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(var.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 From ce1543342774fc5de7fff91eae92db9bf681e989 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sun, 27 Jun 2021 23:56:33 +0200 Subject: [PATCH 2/2] use destination dims --- xarray/core/missing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/missing.py b/xarray/core/missing.py index ca8d35cb938..6b59583e6b1 100644 --- a/xarray/core/missing.py +++ b/xarray/core/missing.py @@ -624,7 +624,7 @@ def interp(var, indexes_coords, method, **kwargs): destination = broadcast_variables(*new_x) interped = interp_func(var.data, x, destination, method, kwargs) - result = Variable(var.dims, interped, attrs=var.attrs) + result = Variable(destination[0].dims, interped, attrs=var.attrs) else: # decompose the interpolation into a succession of independant interpolation