xarray
xarray copied to clipboard
interpolate using quadratic returns nan
What happened?
When using a multiple dimensions xarray (like time, x and y), the quadratic function is not working anymore with interpolate, it returns only nan. slinear is ok. quadratic is only ok when using only a dimension (like only a lat/lon value).
What did you expect to happen?
No response
Minimal Complete Verifiable Example
import xarray as xr
ds = xr.tutorial.load_dataset("ersstv5")
# Generate a subset
subset = ds['sst'].isel(time=slice(0,10),lat=slice(20,40),lon=slice(20,40))
## Plot images
subset.plot(col="time",col_wrap=6)
# Interpolate with quadratic
subset_quadratic = subset.resample(time='10D').interpolate('quadratic')
## subset_quadratic is now full of NaN
subset_quadratic.plot(col="time",col_wrap=6)
# Working with only time dimension
subset_quadratic = subset.isel(lat=0,lon=0).resample(time='10D').interpolate('quadratic')
subset_quadratic.plot()
MVCE confirmation
- [X] Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- [X] Complete example — the example is self-contained, including all data and the text of any traceback.
- [X] Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
- [X] New issue — a search of GitHub Issues suggests this is not a duplicate.
- [X] Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output
No response
Anything else we need to know?
No response
Environment
xarray: 2024.5.0 pandas: 2.1.4 numpy: 1.26.4 scipy: 1.13.0 netCDF4: None pydap: None h5netcdf: 1.3.0 h5py: 3.11.0 zarr: 2.18.0 cftime: None nc_time_axis: None iris: None bottleneck: None dask: 2024.5.0 distributed: 2024.5.0 matplotlib: 3.8.4 cartopy: None seaborn: 0.13.2 numbagg: None fsspec: 2024.3.1 cupy: None pint: None sparse: None flox: None numpy_groupies: None setuptools: 69.5.1 pip: 24.0 conda: None pytest: None mypy: None IPython: 8.24.0 sphinx: 7.3.7
Thanks for opening your first issue here at xarray! Be sure to follow the issue template! If you have an idea for a solution, we would really welcome a Pull Request with proposed changes. See the Contributing Guide for more. It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better. Thank you!
@nkarasiak This one threw me for a loop too. But I believe the issue you're experiencing is due to the presence/absence of nan
values. When using the quadratic or cubic interpolations, if there are any nans
in your dataset, then the returned result is all nans
. I don't know if this is expected behaviour or not. A workaround could be to fill the nans
first (e.g. ds.fillna()
or ds.interpolate_na()
) then conduct your resample+interpolation, or drop coordinates with nans (e.g. ds.dropna(how='all')
)
#load a dataset and subsample - this dataset has nans
ds = xr.tutorial.load_dataset("ersstv5")['sst'].isel(time=slice(0,10),lat=slice(20,40),lon=slice(20,40))
#take the spatial average so we have a 1D datset with no nans
no_nans = ds.mean(['lat','lon'])
#make the last two time-steps nans
with_nans = _1D_no_nans.where(no_nans.time<=pd.to_datetime('1970-08'))
#resample and interpolate
no_nans_upsampled = no_nans.resample(time='D').interpolate('quadratic')
with_nans_upsampled = with_nans.resample(time='D').interpolate('quadratic')
#plot
no_nans.plot(label='original')
no_nans_upsampled.plot(label='upsampled')
with_nans_upsampled.plot(label='upsampled with nans')
plt.legend();
I second @nkarasiak's observation, interpolations of higher order than linear do not work/produce nans, irrespective of the initial presence of nans. I don't know about xarrays exact relationship to scipy, but a wild guess would be that it has something to do with older scipy versions not supporting higher order interpolations when using RegularGridInterpolator (https://docs.scipy.org/doc/scipy-0.18.0/reference/generated/scipy.interpolate.RegularGridInterpolator.html#scipy.interpolate.RegularGridInterpolator), whereas newer do (https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.RegularGridInterpolator.html#regulargridinterpolator)