diff options
author | Tyler Reddy <tyler.je.reddy@gmail.com> | 2023-02-23 17:00:43 -0700 |
---|---|---|
committer | Tyler Reddy <tyler.je.reddy@gmail.com> | 2023-02-23 17:32:01 -0700 |
commit | c61900d5675d4a33a893da5c8e343b6c0c1666dd (patch) | |
tree | 1f2303ce262256f8e7b62af07575f016d2c3a1bb /numpy/ma/core.py | |
parent | 56eee255dba30eeeb084098b37554052c64e84b5 (diff) | |
download | numpy-c61900d5675d4a33a893da5c8e343b6c0c1666dd.tar.gz |
BUG: masked array proper deepcopies
Fixes #22556
Fixes #21022
* add regression test and fix for gh-22556, where
we were relying on the array `copy` arg to deepcopy
a compound object type; I thought about performance issues
here, but if you are already in the land of `object` and
you are explicitly opting in to `deepcopy`, it seems like
performance might be wishful thinking anyway
* add regression test and fix for gh-21022--this one was
weirder but seems possible to sidestep by not trying
to assign a shape of `()` to something that already has
shape `()` and a non-writeable `shape` attribute
Diffstat (limited to 'numpy/ma/core.py')
-rw-r--r-- | numpy/ma/core.py | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index b76ff8ea9..795e79e00 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -2870,7 +2870,13 @@ class MaskedArray(ndarray): _data._mask = _data._mask.copy() # Reset the shape of the original mask if getmask(data) is not nomask: - data._mask.shape = data.shape + # gh-21022 encounters an issue here + # because data._mask.shape is not writeable, but + # the op was also pointless in that case, because + # the shapes were the same, so we can at least + # avoid that path + if data._mask.shape != data.shape: + data._mask.shape = data.shape else: # Case 2. : With a mask in input. # If mask is boolean, create an array of True or False @@ -6300,6 +6306,13 @@ class MaskedArray(ndarray): memo[id(self)] = copied for (k, v) in self.__dict__.items(): copied.__dict__[k] = deepcopy(v, memo) + # as clearly documented for np.copy(), you need to use + # deepcopy() directly for arrays of object type that may + # contain compound types--you cannot depend on normal + # copy semantics to do the right thing here + if self.dtype.type is np.object_: + for idx, _ in enumerate(copied): + copied[idx] = deepcopy(copied[idx]) return copied |