question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

copy / deepcopy not deepcopying coords?

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
pletchmcommented, Apr 12, 2019

I’d like to take a shot fixing this bug unless someone else already is working on it. Would that be alright?

1reaction
max-sixtycommented, Apr 12, 2019

Great @pletchm ! This is a example of a recent similar issue: https://github.com/pydata/xarray/pull/2839/files

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found