pydata / xarray

N-D labeled arrays and datasets in Python
https://xarray.dev
Apache License 2.0
3.5k stars 1.04k forks source link

interpolate using quadratic returns nan #9028

Open nkarasiak opened 1 month ago

nkarasiak commented 1 month ago

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

Relevant log output

No response

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS ------------------ commit: None python: 3.12.3 | packaged by conda-forge | (main, Apr 15 2024, 18:20:11) [MSC v.1938 64 bit (AMD64)] python-bits: 64 OS: Windows OS-release: 11 machine: AMD64 processor: Intel64 Family 6 Model 186 Stepping 3, GenuineIntel byteorder: little LC_ALL: None LANG: en LOCALE: ('fr_FR', 'cp1252') libhdf5: 1.14.3 libnetcdf: None 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
welcome[bot] commented 1 month ago

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!

cbur24 commented 2 weeks ago

@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();