Interpolation behaviour inconsistent with numpy?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
Hey all,
When running dataset.interp(time=dataset.time) fills with np.nan if one of the neighbor is a np.nan even when interpolation is not actually needed.
Here is the sample code to reproduce the issue :
def test_crop_times_nan() :
ds = xr.Dataset(
data_vars = {
"some_variable" : (['x', 'time'], np.array([[np.nan, 0, 1]]))
},
coords = {
"time" : np.array([0,1,2])
}
)
result = ds.interp(time=ds.time)
# result["some_variable"].value == [nan, nan, 1.0]
# whereas [nan, 0, 1.0] is EXPECTED
xr.testing.assert_allclose(ds, result)
Please note that numpy does not have the same behavior :
>>> import numpy as np
>>> np.interp([0,1,2], xp=[0,1,2], fp=[np.nan,0,1])
array([nan, 0., 1.])
Is that an intended behaviour for xarray?
If so, does this mean that I first have to check if an interpolation is needed instead of doing it no matter what (and use reindex instead of interp if it is not needed) ?
(this will be kind of tricky if interpolation is needed for certain values and some not...)
Thanks for your help ;)
Environment:
Output of xr.show_versions()
INSTALLED VERSIONS ------------------ commit: None python: 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0] python-bits: 64 OS: Linux OS-release: 5.8.0-7642-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: ('en_US', 'UTF-8') libhdf5: 1.12.0 libnetcdf: 4.7.4xarray: 0.18.2
pandas: 1.2.4
numpy: 1.19.4
scipy: 1.6.0
netCDF4: 1.5.6
pydap: None
h5netcdf: 0.8.1
h5py: 3.1.0
Nio: None
zarr: None
cftime: 1.3.0
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2021.01.0
distributed: 2021.01.0
matplotlib: 3.4.2
cartopy: None
seaborn: None
numbagg: None
pint: None
setuptools: 57.4.0
pip: 20.2.4
conda: None
pytest: None
IPython: 7.19.0
sphinx: None
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided Dataset.interp(time=dataset.time) reproducer and compare it with np.interp and xr.testing.assert_allclose. Trace the Dataset.interp path for coordinates that already match, then add a regression test showing that existing NaN values are preserved when no interpolation is needed; done means the result is [nan, 0, 1].
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100