diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2015-06-17 23:16:50 -0400 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2015-06-17 23:16:50 -0400 |
commit | 2c29b138aedb1774e7f984f4a237243455c423ad (patch) | |
tree | 3fbaa752070c48a1972acf8ac857db35cea0e217 /numpy | |
parent | a580d6fb832ee1c571a60b104e44b6b21f3c2951 (diff) | |
parent | 1a4d94357eb8d06930ad69309fda579a8caa6c57 (diff) | |
download | numpy-2c29b138aedb1774e7f984f4a237243455c423ad.tar.gz |
Merge pull request #5962 from mhvk/ma-getitem-nomask-correction
BUG Ensure masked object arrays can always return single items.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/ma/core.py | 6 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 12 |
2 files changed, 18 insertions, 0 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 24e18a9d6..703580d27 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -3046,6 +3046,7 @@ class MaskedArray(ndarray): # mask of being reshaped if it hasn't been set up properly yet... # So it's easier to stick to the current version _mask = self._mask + # Did we extract a single item? if not getattr(dout, 'ndim', False): # A record ................ if isinstance(dout, np.void): @@ -3057,6 +3058,11 @@ class MaskedArray(ndarray): # Just a scalar............ elif _mask is not nomask and _mask[indx]: return masked + elif self.dtype.type is np.object_ and self.dtype is not dout.dtype: + # self contains an object array of arrays (yes, that happens). + # If masked, turn into a MaskedArray, with everything masked. + if _mask is not nomask and _mask[indx]: + return MaskedArray(dout, mask=True) else: # Force dout to MA ........ dout = dout.view(type(self)) diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 9cc784d02..2bf782724 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -685,6 +685,18 @@ class TestMaskedArray(TestCase): finally: masked_print_option.set_display(ini_display) + def test_object_with_array(self): + mx1 = masked_array([1.], mask=[True]) + mx2 = masked_array([1., 2.]) + mx = masked_array([mx1, mx2], mask=[False, True]) + assert mx[0] is mx1 + assert mx[1] is not mx2 + assert np.all(mx[1].data == mx2.data) + assert np.all(mx[1].mask) + # check that we return a view. + mx[1].data[0] = 0. + assert mx2[0] == 0. + #------------------------------------------------------------------------------ class TestMaskedArrayArithmetic(TestCase): |