copy / deepcopy not deepcopying coords?
See original GitHub issueI don’t know if this is intentional, I thought that arr.copy(deep=True) or deepcopy(arr) would give me completely independent copies of a DateArray, but this seems not be the case?
>>> import xarray as xr
>>> xarr1 = xr.DataArray([1,2], coords=dict(x=[0,1]), dims=('x',))
>>> xarr1.x.data[0]
0
>>> xarr2 = xarr1.copy(deep=True) #xarr2 = deepcopy(xarr1) -> leads to same result
>>> xarr2.x.data[0] = -1
>>> xarr1.x.data[0]
-1
How can I create completely independent copies of a DateArray? I wrote a function for this, but don’t know if this really always does what I expect and if there is a more elegant way?
def deepcopy_xarr(xarr):
"""
Deepcopy for xarray that makes sure coords and attrs
are properly deepcopied.
With normal copy method from xarray, when i mutated
xarr.coords[coord].data it would also mutate in the copy
and vice versa.
Parameters
----------
xarr: DateArray
Returns
-------
xcopy: DateArray
Deep copy of xarr
"""
xcopy = xarr.copy(deep=True)
for dim in xcopy.coords:
xcopy.coords[dim].data = np.copy(xcopy.coords[dim].data)
xcopy.attrs = deepcopy(xcopy.attrs)
for attr in xcopy.attrs:
xcopy.attrs[attr] = deepcopy(xcopy.attrs[attr])
return xcopy
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
copy — Shallow and deep copy operations — Python 3.11.1 ...
For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the...
Read more >python copy.deepcopy lists seems shallow - Stack Overflow
The problem is that deepcopy keeps a memo that contains all instances that have been copied already. That's to avoid infinite recursions and ......
Read more >Shallow vs Deep Copying of Python Objects
Because our point object uses immutable types (ints) for its coordinates, there's no difference between a shallow and a deep copy in this...
Read more >copy in Python (Deep Copy and Shallow Copy) - GeeksforGeeks
In the case of deep copy, a copy of the object is copied into another object. It means that any changes made to...
Read more >DeepCopy of DataSet does not copy cells. - VTK Discourse
Hi, I'm writing a ParaView filter in Python, and I need the output dataset to have the ... only the point coordinates should...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I’d like to take a shot fixing this bug unless someone else already is working on it. Would that be alright?
Great @pletchm ! This is a example of a recent similar issue: https://github.com/pydata/xarray/pull/2839/files