Skip to content

Commit 0b6716d

Browse files
FIX: adapt handling of copy keyword argument in scipy backend for numpy >= 2.0dev (#8851)
* FIX: adapt handling of copy keyword argument in scipy backend for numpy >= 2.0dev FIX: adapt handling of copy keyword argument in scipy backend for numpy >= 2.0dev * Add whats-new.rst entry * Apply suggestions from code review --------- Co-authored-by: Deepak Cherian <[email protected]>
1 parent 61ffc38 commit 0b6716d

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

doc/whats-new.rst

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Bug fixes
5959
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
6060
- do not cast `_FillValue`/`missing_value` in `CFMaskCoder` if `_Unsigned` is provided
6161
(:issue:`8844`, :pull:`8852`).
62+
- Adapt handling of copy keyword argument in scipy backend for numpy >= 2.0dev
63+
(:issue:`8844`, :pull:`8851`).
6264
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
6365

6466
Documentation

xarray/backends/scipy_.py

+10
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
Frozen,
2929
FrozenDict,
3030
close_on_error,
31+
module_available,
3132
try_read_magic_number_from_file_or_path,
3233
)
3334
from xarray.core.variable import Variable
@@ -39,6 +40,9 @@
3940
from xarray.core.dataset import Dataset
4041

4142

43+
HAS_NUMPY_2_0 = module_available("numpy", minversion="2.0.0.dev0")
44+
45+
4246
def _decode_string(s):
4347
if isinstance(s, bytes):
4448
return s.decode("utf-8", "replace")
@@ -76,6 +80,12 @@ def __getitem__(self, key):
7680
# with the netCDF4 library by ensuring we can safely read arrays even
7781
# after closing associated files.
7882
copy = self.datastore.ds.use_mmap
83+
84+
# adapt handling of copy-kwarg to numpy 2.0
85+
# see https://github.com/numpy/numpy/issues/25916
86+
# and https://github.com/numpy/numpy/pull/25922
87+
copy = None if HAS_NUMPY_2_0 and copy is False else copy
88+
7989
return np.array(data, dtype=self.dtype, copy=copy)
8090

8191
def __setitem__(self, key, value):

0 commit comments

Comments
 (0)