>>> da = xr.DataArray([1., 2.], coords=[('dim', [.5, 1.])])
>>> da.coords['nondim'] = ('dim', [0., 1.])
>>> da
<xarray.DataArray (dim: 2)>
array([1., 2.])
Coordinates:
* dim (dim) float64 0.5 1.0
nondim (dim) float64 0.0 1.0
>>> da.interp(dim=.75)
<xarray.DataArray ()>
array(1.5)
Coordinates:
nondim float64 0.5
dim float64 0.75
>>> da.interp(nondim=.5)
Traceback (most recent call last):
File "<ipython-input-192-e3df34cff90f>", line 1, in <module>
da.interp(nondim=.5)
File "/usr/lib64/python3.6/site-packages/xarray/core/dataarray.py", line 951, in interp
**coords_kwargs)
File "/usr/lib64/python3.6/site-packages/xarray/core/dataset.py", line 1860, in interp
indexers = OrderedDict(self._validate_indexers(coords))
File "/usr/lib64/python3.6/site-packages/xarray/core/dataset.py", line 1316, in _validate_indexers
raise ValueError("dimensions %r do not exist" % invalid)
ValueError: dimensions ['nondim'] do not exist
The same interpolation as for 'dim'.
Apparently, xarray currently cannot interpolate using non-dimension coordinates.
xr.show_versions()
commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.19.72-gentoo
machine: x86_64
processor: Intel(R) Core(TM) i7-2620M CPU @ 2.70GHz
byteorder: little
LC_ALL: None
LANG: nl_BE.UTF-8
LOCALE: nl_BE.UTF-8
xarray: 0.10.8
pandas: 0.24.2
numpy: 1.14.5
scipy: 1.1.0
netCDF4: 1.5.1.2
h5netcdf: None
h5py: 2.9.0
Nio: None
zarr: None
bottleneck: 1.2.1
cyordereddict: None
dask: 1.2.0
distributed: None
matplotlib: 2.2.2
cartopy: None
seaborn: None
setuptools: 40.6.3
pip: 19.1
conda: None
pytest: 3.10.1
IPython: 5.4.1
sphinx: 1.7.5
you might be able to achieve what you want by using swap_dims:
da.swap_dims({"dim": "nondim"}).interp(nondim=.5)
you might be able to achieve what you want by using
swap_dims
Yes, that is a nice workaround (I used another, less convenient one). But I would say that the current (lack of) functionality is a bug: the documentation talks about coordinates, not about dimensions. Moreover, it seems like something one would want to support. I do not know how complicated this would be to implement, but if reasonably feasible, I would request to keep this issue open.
This is an example of a general UI problem which I was actually about to raise an issue about.
There are loads of places in xarray's API which require a dimension, but the operation would make just as much sense if a 1D coordinate were supplied instead (as that uniquely specifies a dimension).
I encountered the same kind of problem in #3334 with da.transpose().
I wonder if it would be possible to have a little helper function process the dim input to these functions and if it's a 1D coord, just demote it to the corresponding dim.
More generally, this is about applying operations with non dimensional coordinates which need not be 1D. Thinking about API, how about adding a coord kwarg?
da.differentiate(dim="nx", coord="x")
This says use np.diff on da along dimension nx and divide by spacing calculated by np.diff on da.x along dimension nx
If x is 1D, then da.differentiate(coord="x") is equivalent to da.differentiate(dim="nx", coord="x") which is your proposal. i think this is cleaner and more explicit than automatically mapping 1D nondimensional coords to dimensions.
Most helpful comment
More generally, this is about applying operations with non dimensional coordinates which need not be 1D. Thinking about API, how about adding a
coordkwarg?da.differentiate(dim="nx", coord="x")This says use
np.diffondaalong dimensionnxand divide by spacing calculated bynp.diffonda.xalong dimensionnxIf
xis 1D, thenda.differentiate(coord="x")is equivalent toda.differentiate(dim="nx", coord="x")which is your proposal. i think this is cleaner and more explicit than automatically mapping 1D nondimensional coords to dimensions.