Coordinates not deep copy
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
What happened?
The coordinates are not copied if you perform deepcopy for xarray.
This issue was fixed before. I don't see it, for example, in xarray 2022.12.0, but in the latest version the issue is there again.
import xarray as xr
xarr1 = xr.DataArray(
np.zeros([2]),
coords=dict(x=[0.0, 1.0]), # important to use 'float' here! with 'int' it is working fine
dims=("x")
)
print(xarr1.x.data[0]) # 0.0
xarr2 = xarr1.copy(deep=True)
xarr2.x.data[0] = 45
print(xarr1.x.data[0]) # gives 45
Interesting, that if your coordinates are int, then the issue is gone
What did you expect to happen?
import xarray as xr
xarr1 = xr.DataArray(
np.zeros([2]),
coords=dict(x=[0.0, 1.0]), # important to use 'float' here! with 'int' it is working fine
dims=("x")
)
print(xarr1.x.data[0]) # 0.0
xarr2 = xarr1.copy(deep=True)
xarr2.x.data[0] = 45
print(xarr1.x.data[0]) # I expect it to be 0.0
Minimal Complete Verifiable Example
import xarray as xr
xarr1 = xr.DataArray(
np.zeros([2]),
coords=dict(x=[0.0, 1.0]), # important to use 'float' here! with 'int' it is working fine
dims=("x")
)
print(xarr1.x.data[0]) # 0.0
xarr2 = xarr1.copy(deep=True)
xarr2.x.data[0] = 45
print(xarr1.x.data[0]) # gives 45
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.
Relevant log output
No response
Anything else we need to know?
No response
Environment
python 3.8.10
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the reported DataArray deep-copy example against the current xarray version and compare the float-coordinate behavior with integer coordinates. Trace the copy path involved in coordinate data and add a regression test showing that changing xarr2.x.data does not change xarr1.x.data; done when the original remains 0.0.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100