-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
What happened?
Interpolation of data arrays with time informatively errors:
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "xarray[complete]@git+https://github.com/pydata/xarray.git@main",
# ]
# ///
import numpy as np
import xarray as xr
da = xr.DataArray(
data=(np.array([1, 4, 2]).astype("datetime64[ns]")),
dims=("x",),
coords={"x": [0, 1, 2]},
)
da_interp = da.interp(x=[0, 0.75, 1.25, 1.75])Traceback (most recent call last):
File "/Users/Hodgs004/coding/repos/parcels/xarray-repro/min-rep-da.py", line 18, in <module>
da_interp = da.interp(x=[0, 0.75, 1.25, 1.75])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/Hodgs004/.cache/uv/environments-v2/min-rep-da-138593a3f0628b04/lib/python3.12/site-packages/xarray/core/dataarray.py", line 2411, in interp
raise TypeError(
TypeError: interp only works for a numeric type array. Given datetime64[ns].
However when doing it with a Dataset, the time array is silently dropped with no warnings.
What did you expect to happen?
If it's expected behaviour for it to be dropped, then it would be nice for it to emit a warning or something
Minimal Complete Verifiable Example
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "xarray[complete]@git+https://github.com/pydata/xarray.git@main",
# ]
# ///
#
# This script automatically imports the development branch of xarray to check for issues.
# Please delete this header if you have _not_ tested this script with `uv run`!
import numpy as np
import xarray as xr
ds = xr.Dataset.from_dict(
{
"coords": {},
"dims": {"trajectory": 10, "obs": 10},
"data_vars": {
"something": {
"data": np.random.rand(10, 10).astype("float64"),
"dims": ["trajectory", "obs"],
},
"time": {
"data": np.random.rand(10, 10).astype("datetime64[ns]"),
"dims": ["trajectory", "obs"],
},
},
}
)
print("Original dataset:\n", ds)
assert "time" in ds.data_vars
# following https://docs.xarray.dev/en/stable/user-guide/interpolation.html#advanced-interpolation
# for Advanced Interpolation and indexing
trajectory = ds.trajectory.rename({"trajectory": "z"})
obs = ds.obs.rename({"obs": "z"})
ds_interp = ds.interp(trajectory=trajectory, obs=obs)
print("Interped dataset:\n", ds_interp)
assert "something" in ds_interp.data_vars
assert "time" in ds_interp.data_vars, "Why is 'time' missing from the variables?"Steps to reproduce
Run script. MCVE output:
Original dataset:
<xarray.Dataset> Size: 2kB
Dimensions: (trajectory: 10, obs: 10)
Dimensions without coordinates: trajectory, obs
Data variables:
something (trajectory, obs) float64 800B 0.05063 0.03253 ... 0.8666 0.4773
time (trajectory, obs) datetime64[ns] 800B 1970-01-01 ... 1970-01-01
Interped dataset:
<xarray.Dataset> Size: 240B
Dimensions: (z: 10)
Dimensions without coordinates: z
Data variables:
something (z) float64 80B 0.05063 0.9336 0.6782 ... 0.2084 0.466 0.4773
trajectory (z) int64 80B 0 1 2 3 4 5 6 7 8 9
obs (z) int64 80B 0 1 2 3 4 5 6 7 8 9
Traceback (most recent call last):
File "/Users/Hodgs004/coding/repos/parcels/xarray-repro/min-rep.py", line 42, in <module>
assert "time" in ds_interp.data_vars, "Why is 'time' missing from the variables?"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Why is 'time' missing from the variables?
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
Anything else we need to know?
No response
Environment
commit: None
python: 3.12.10 | packaged by conda-forge | (main, Apr 10 2025, 22:19:24) [Clang 18.1.8 ]
python-bits: 64
OS: Darwin
OS-release: 24.6.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: (None, 'UTF-8')
libhdf5: 1.14.6
libnetcdf: 4.9.3
xarray: 2025.10.2.dev33+g6da8c2c92
pandas: 2.3.3
numpy: 2.3.4
scipy: 1.16.3
netCDF4: 1.7.3
pydap: 3.5.8
h5netcdf: 1.7.3
h5py: 3.15.1
zarr: 3.1.3
cftime: 1.6.5
nc_time_axis: 1.4.1
iris: None
bottleneck: 1.6.0
dask: 2025.11.0
distributed: 2025.11.0
matplotlib: 3.10.7
cartopy: 0.25.0
seaborn: 0.13.2
numbagg: 0.9.3
fsspec: 2025.10.0
cupy: None
pint: None
sparse: 0.17.0
flox: 0.10.7
numpy_groupies: 0.11.3
setuptools: None
pip: None
conda: None
pytest: None
mypy: None
IPython: None
sphinx: None
None