Skip to content

Commit 1a4d943

Browse files
committed
BUG Ensure masked object arrays can always return single items.
For a masked array holding objects that themselves are arrays, when selecting a single item, it is treated as if it were a slice of the array and an attempt is made to set its mask. This was always a bug, but it become visible with a recent change to `MaskedArray.__getitem__` (gh-4586) where it is attempted to change the shape of the mask. With this PR, this case gets special treatment in that the object is made into a Masked Array with a full set mask. (A previous attempt to do the perhaps more logical think and just return `masked` caused quite a few errors in astropy.io.votable; it seemed it was not worth breaking backwards compatibility that much). Test case that now works but used to fail: ``` mx1 = MaskedArray([1.], mask=[True]) mx2 = MaskedArray([1., 2.]) mx = MaskedArray([mx1, mx2], mask=[False, True]) mx[0] ```
1 parent a580d6f commit 1a4d943

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎doc/release/1.10.0-notes.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ byte-array indices now raises an IndexError
9999
Indexing an ndarray using a byte-string in Python 3 now raises an IndexError
100100
instead of a ValueError.
101101

102+
Masked arrays containing objects with arrays
103+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
104+
For such (rare) masked arrays, getting a single masked item no longer returns a
105+
corrupted masked array, but a fully masked version of the item.
106+
102107
New Features
103108
============
104109

‎numpy/ma/core.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3046,6 +3046,7 @@ def __getitem__(self, indx):
30463046
# mask of being reshaped if it hasn't been set up properly yet...
30473047
# So it's easier to stick to the current version
30483048
_mask = self._mask
3049+
# Did we extract a single item?
30493050
if not getattr(dout, 'ndim', False):
30503051
# A record ................
30513052
if isinstance(dout, np.void):
@@ -3057,6 +3058,11 @@ def __getitem__(self, indx):
30573058
# Just a scalar............
30583059
elif _mask is not nomask and _mask[indx]:
30593060
return masked
3061+
elif self.dtype.type is np.object_ and self.dtype is not dout.dtype:
3062+
# self contains an object array of arrays (yes, that happens).
3063+
# If masked, turn into a MaskedArray, with everything masked.
3064+
if _mask is not nomask and _mask[indx]:
3065+
return MaskedArray(dout, mask=True)
30603066
else:
30613067
# Force dout to MA ........
30623068
dout = dout.view(type(self))

‎numpy/ma/tests/test_core.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,18 @@ def test_mvoid_print(self):
685685
finally:
686686
masked_print_option.set_display(ini_display)
687687

688+
def test_object_with_array(self):
689+
mx1 = masked_array([1.], mask=[True])
690+
mx2 = masked_array([1., 2.])
691+
mx = masked_array([mx1, mx2], mask=[False, True])
692+
assert mx[0] is mx1
693+
assert mx[1] is not mx2
694+
assert np.all(mx[1].data == mx2.data)
695+
assert np.all(mx[1].mask)
696+
# check that we return a view.
697+
mx[1].data[0] = 0.
698+
assert mx2[0] == 0.
699+
688700

689701
#------------------------------------------------------------------------------
690702
class TestMaskedArrayArithmetic(TestCase):

0 commit comments

Comments
 (0)