You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
darr["x"] does not share memory with some_array, which I check with np.shares_memory(darr["x"].data, some_array). This is surprising because another_array shares data with darr.data, and I'd expect coordinate to behave the same. Moreover, if I do:
darr.coords["x"] = ("x", some_array)
They do share the underlying data.
This is not a huge blocker for us, since the workaround is fairly straightforward, but I thought it may be an unexpected behavior.
What did you expect to happen?
I expect no copy to happen both for data and coordinates when I create DataArrays.
Minimal Complete Verifiable Example
importxarrayasxrimportnumpyasnpdeftest_data_array_coord_array_not_copied_when_assigning():
arr=np.random.random(2)
arr_coord=np.arange(2)
darr=xr.DataArray(arr, dims=("x"))
assertdarr.dataisarr# Variable is the same as input array.darr.coords["x"] = ("x", arr_coord)
assertnp.shares_memory(darr["x"].data, arr_coord)
deftest_data_array_coord_array_not_copied_when_providing_at_initialization():
arr=np.random.random(2)
arr_coord=np.arange(2)
darr_coord=xr.DataArray(arr_coord, dims="x")
assertdarr_coord.dataisarr_coord# Variable is the same as input array.darr=xr.DataArray(arr, coords={"x": darr_coord}, dims=("x"))
assertdarr.dataisarr# Variable is the same as input array.assertnp.shares_memory(darr["x"].data, arr_coord)
test_data_array_coord_array_not_copied_when_assigning() # <- Bueno!test_data_array_coord_array_not_copied_when_providing_at_initialization() # <- Sad :(
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.
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!
What happened?
Hi there! I am designing an internal library where I want to provide some utilities with some guarantees on the amount of copies are happening.
I noticed that if I do:
darr["x"]
does not share memory withsome_array
, which I check withnp.shares_memory(darr["x"].data, some_array)
. This is surprising becauseanother_array
shares data withdarr.data
, and I'd expect coordinate to behave the same. Moreover, if I do:darr.coords["x"] = ("x", some_array)
They do share the underlying data.
This is not a huge blocker for us, since the workaround is fairly straightforward, but I thought it may be an unexpected behavior.
What did you expect to happen?
I expect no copy to happen both for data and coordinates when I create DataArrays.
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
No response
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: None
python: 3.9.19 (main, Aug 14 2024, 05:11:09)
[Clang 18.1.8 ]
python-bits: 64
OS: Linux
OS-release: 6.8.0-51-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: None
libnetcdf: None
xarray: 2024.7.0
pandas: 2.2.3
numpy: 2.0.2
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
zarr: None
cftime: None
nc_time_axis: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: None
pip: None
conda: None
pytest: 8.3.4
mypy: 1.13.0
IPython: None
sphinx: None
None
The text was updated successfully, but these errors were encountered: