Skip to content

Surprising deepcopy semantics with dtype='object' #4362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
darikg opened this issue Aug 21, 2020 · 4 comments · Fixed by #4379
Closed

Surprising deepcopy semantics with dtype='object' #4362

darikg opened this issue Aug 21, 2020 · 4 comments · Fixed by #4379
Labels
bug topic-arrays related to flexible array support

Comments

@darikg
Copy link
Contributor

darikg commented Aug 21, 2020

from copy import deepcopy
import numpy as np
import xarray as xr

class Dummy: pass

a0 = np.array([Dummy()])
a1 = deepcopy(a0)
print(a0[0] is a1[0])  # False, as expected

x0 = xr.DataArray(a0, dims='dummy')
x1 = deepcopy(x0)
print(x0.values[0] is x1.values[0])  # unexpectedly True

I think this is a bug, and would be fixed with an extra deepcopy around here

@keewis
Copy link
Collaborator

keewis commented Aug 21, 2020

the issue is that we use numpy.ndarray.copy to copy the data:

data = data.copy()
(numpy.ndarray defines __array_function__).

You can reproduce the issue using just numpy:

In [14]: a0.copy()[0] is a0[0]                                                                                  
Out[14]: True

We might want to use deepcopy instead because duck arrays are not required to implement .copy.

@darikg darikg changed the title Surpising deepcopy semantics with dtype='object' Surprising deepcopy semantics with dtype='object' Aug 21, 2020
@dcherian dcherian added the topic-arrays related to flexible array support label Aug 21, 2020
@shoyer
Copy link
Member

shoyer commented Aug 23, 2020

I agree, marking this as a bug. We should use deepcopy() recursively on NumPy arrays.

@shoyer shoyer added the bug label Aug 23, 2020
@mathause
Copy link
Collaborator

@darikg are you interested to send in a PR?

@darikg
Copy link
Contributor Author

darikg commented Aug 26, 2020

Sure! I'll be back in a couple days when I get a chance

darikg added a commit to darikg/xarray that referenced this issue Aug 26, 2020
darikg added a commit to darikg/xarray that referenced this issue Aug 27, 2020
max-sixty pushed a commit that referenced this issue Aug 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug topic-arrays related to flexible array support
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants