Skip to content

Order of coordinates not preserved when calling interp #10706

@schmoelder

Description

@schmoelder

What happened?

When calling the interp method on DataArrays or Datasets, the order of the coordinates is not preserved.

What did you expect to happen?

Keep order of coordinates.

Minimal Complete Verifiable Example

import numpy as np
import xarray as xr

# %% DataArray
coordinates={
    "x": np.linspace(0, 1, 10),
    "y": np.linspace(0, 1, 5),
    "time": np.linspace(0, 1, 6),
}
grid_shape = tuple(len(v) for v in coordinates.values())

data = np.zeros(grid_shape)

data_array = xr.DataArray(data=data, coords=coordinates)

print(data_array.to_numpy().shape)
print(data_array.coords)

data_array_interp = data_array.interp(x=np.linspace(0, 1, 20))
print(data_array_interp.to_numpy().shape)
print(data_array_interp.coords)

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.

Relevant log output

(10, 5, 6)
Coordinates:
  * x        (x) float64 80B 0.0 0.1111 0.2222 0.3333 ... 0.7778 0.8889 1.0
  * y        (y) float64 40B 0.0 0.25 0.5 0.75 1.0
  * time     (time) float64 48B 0.0 0.2 0.4 0.6 0.8 1.0
(20, 5, 6)
Coordinates:
  * y        (y) float64 40B 0.0 0.25 0.5 0.75 1.0
  * time     (time) float64 48B 0.0 0.2 0.4 0.6 0.8 1.0
  * x        (x) float64 160B 0.0 0.05263 0.1053 0.1579 ... 0.8947 0.9474 1.0

Anything else we need to know?

Note, the shape is as expected, but the order of the coordinates has been modified.

The same holds for datasets:

# %% Dataset
components = ["A", "B"]

coordinates={
    "x": np.linspace(0, 1, 10),
    "y": np.linspace(0, 1, 5),
    "time": np.linspace(0, 1, 6),
}
grid_shape = tuple(len(v) for v in coordinates.values())

data = np.zeros((len(components), *grid_shape))

data_vars = {
    comp: xr.DataArray(data=data[i, :], coords=coordinates)
    for i, comp in enumerate(components)
}
data_set = xr.Dataset(data_vars=data_vars, coords=coordinates)
print(data_set.to_array().shape)
print(data_set.coords)

data_set_interp = data_set.interp(x=np.linspace(0, 1, 20))
print(data_set_interp.to_array().shape)
print(data_set_interp.coords)

Output:

(2, 10, 5, 6)
Coordinates:
  * x        (x) float64 80B 0.0 0.1111 0.2222 0.3333 ... 0.7778 0.8889 1.0
  * y        (y) float64 40B 0.0 0.25 0.5 0.75 1.0
  * time     (time) float64 48B 0.0 0.2 0.4 0.6 0.8 1.0
(2, 20, 5, 6)
Coordinates:
  * y        (y) float64 40B 0.0 0.25 0.5 0.75 1.0
  * time     (time) float64 48B 0.0 0.2 0.4 0.6 0.8 1.0
  * x        (x) float64 160B 0.0 0.05263 0.1053 0.1579 ... 0.8947 0.9474 1.0
(10, 5, 6)
Coordinates:
  * x        (x) float64 80B 0.0 0.1111 0.2222 0.3333 ... 0.7778 0.8889 1.0
  * y        (y) float64 40B 0.0 0.25 0.5 0.75 1.0
  * time     (time) float64 48B 0.0 0.2 0.4 0.6 0.8 1.0
(20, 5, 6)
Coordinates:
  * y        (y) float64 40B 0.0 0.25 0.5 0.75 1.0
  * time     (time) float64 48B 0.0 0.2 0.4 0.6 0.8 1.0
  * x        (x) float64 160B 0.0 0.05263 0.1053 0.1579 ... 0.8947 0.9474 1.0

Environment

INSTALLED VERSIONS

commit: None
python: 3.12.11 | packaged by conda-forge | (main, Jun 4 2025, 14:45:31) [GCC 13.3.0]
python-bits: 64
OS: Linux
OS-release: 6.14.0-29-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: de_DE.UTF-8
LOCALE: ('C', 'UTF-8')
libhdf5: 1.14.3
libnetcdf: None

xarray: 2025.8.0
pandas: 2.3.2
numpy: 2.2.6
scipy: 1.16.1
netCDF4: None
pydap: None
h5netcdf: 1.6.4
h5py: 3.13.0
zarr: None
cftime: None
nc_time_axis: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.10.5
cartopy: None
seaborn: None
numbagg: None
fsspec: 2025.5.1
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 80.9.0
pip: 25.2
conda: None
pytest: 8.4.1
mypy: None
IPython: 8.37.0
sphinx: 8.3.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions