Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storing results of interpolate_na into numpy.array #9312

Closed
3 of 5 tasks
Breeze-Hu opened this issue Aug 4, 2024 · 3 comments · Fixed by #9393
Closed
3 of 5 tasks

storing results of interpolate_na into numpy.array #9312

Breeze-Hu opened this issue Aug 4, 2024 · 3 comments · Fixed by #9393

Comments

@Breeze-Hu
Copy link

Breeze-Hu commented Aug 4, 2024

What happened?

Storing the interpolated results into numpy.zero will prompt a problem. But I'm not quite sure if it's due to xarray or numpy 2.0. Show: DeprecationWarning: array implementation doesn't accept a copy keyword……

And the problem doesn't occur in one dimensional arrays, but it does in two dimensions.

What did you expect to happen?

No response

Minimal Complete Verifiable Example

import numpy as np
import Xarray as xr

f = np.zeros((2,5))
print(f)
da = xr.DataArray(
    [np.nan, 2, 3, np.nan, 0], dims="x", coords={"x": [0, 1, 2, 3, 4]}
)
f[0,:] = da.interpolate_na(dim="x", method="linear")
print(da.interpolate_na(dim="x", method="linear"))
print(f)

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

No response

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS

commit: None
python: 3.11.9 | packaged by conda-forge | (main, Apr 19 2024, 18:34:54) [Clang 16.0.6 ]
python-bits: 64
OS: Darwin
OS-release: 23.6.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: None
LANG: None
LOCALE: (None, 'UTF-8')
libhdf5: 1.14.3
libnetcdf: 4.9.2

xarray: 2024.7.0
pandas: 2.2.2
numpy: 2.0.1
scipy: 1.14.0
netCDF4: 1.7.1
pydap: None
h5netcdf: 1.3.0
h5py: 3.11.0
zarr: None
cftime: 1.6.4
nc_time_axis: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.9.1
cartopy: 0.23.0
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 72.1.0
pip: 24.2
conda: None
pytest: None
mypy: None
IPython: 8.26.0
sphinx: None

@Breeze-Hu Breeze-Hu added bug needs triage Issue that has not been reviewed by xarray team member labels Aug 4, 2024
Copy link

welcome bot commented Aug 4, 2024

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!

@kmuehlbauer
Copy link
Contributor

@Breeze-Hu Thanks for bringing this to attention. It looks like we need to add copy to the DataArray.__array__-method. See also discussion in numpy/numpy#25941.

I'm wondering if it is enough to add copy here?

def __array__(self: Any, dtype: DTypeLike | None = None) -> np.ndarray:
return np.asarray(self.values, dtype=dtype)

like so:

    def __array__(self: Any, dtype: DTypeLike | None = None, copy: bool | None = None) -> np.ndarray:
        return np.asarray(self.values, dtype=dtype, copy=copy)

@kmuehlbauer kmuehlbauer added topic-internals and removed bug needs triage Issue that has not been reviewed by xarray team member labels Aug 9, 2024
@Breeze-Hu
Copy link
Author

Thanks to the developers for looking at this.
Even though my limited programming skills can't give any advice on the project. But xarray users who encounter the same problem can temporarily sidestep the prompt by using
f[0,:] = da.interpolate_na(dim="x", method="linear").values to deposit the result into the dataframe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants