summaryrefslogtreecommitdiff
path: root/numpy/ma
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2021-03-16 17:15:16 -0600
committerGitHub <noreply@github.com>2021-03-16 17:15:16 -0600
commitb1deaa05346ac03c2a55e66c08eed24350bdf39a (patch)
treedad8f10d4d14ac9b61ce9e83f76075d6581362b6 /numpy/ma
parent9ea0ca93655e11065a360b97baad3d11eb599ddd (diff)
parent54a2c49afaae10ba4de29b465174a184580b3252 (diff)
downloadnumpy-b1deaa05346ac03c2a55e66c08eed24350bdf39a.tar.gz
Merge pull request #18605 from seberg/issue-18551
BUG: Fix ma coercion list-of-ma-arrays if they do not cast to bool
Diffstat (limited to 'numpy/ma')
-rw-r--r--numpy/ma/core.py5
-rw-r--r--numpy/ma/tests/test_core.py20
2 files changed, 23 insertions, 2 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py
index cda2eeb34..10ee0fb06 100644
--- a/numpy/ma/core.py
+++ b/numpy/ma/core.py
@@ -2859,8 +2859,9 @@ class MaskedArray(ndarray):
elif isinstance(data, (tuple, list)):
try:
# If data is a sequence of masked array
- mask = np.array([getmaskarray(np.asanyarray(m, dtype=mdtype))
- for m in data], dtype=mdtype)
+ mask = np.array(
+ [getmaskarray(np.asanyarray(m, dtype=_data.dtype))
+ for m in data], dtype=mdtype)
except ValueError:
# If data is nested
mask = nomask
diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py
index f40780625..9bfb82d1f 100644
--- a/numpy/ma/tests/test_core.py
+++ b/numpy/ma/tests/test_core.py
@@ -238,6 +238,26 @@ class TestMaskedArray:
assert_equal(data, [[0, 1, 2, 3, 4], [4, 3, 2, 1, 0]])
assert_(data.mask is nomask)
+ def test_creation_with_list_of_maskedarrays_no_bool_cast(self):
+ # Tests the regression in gh-18551
+ masked_str = np.ma.masked_array(['a', 'b'], mask=[True, False])
+ normal_int = np.arange(2)
+ res = np.ma.asarray([masked_str, normal_int], dtype="U21")
+ assert_array_equal(res.mask, [[True, False], [False, False]])
+
+ # The above only failed due a long chain of oddity, try also with
+ # an object array that cannot be converted to bool always:
+ class NotBool():
+ def __bool__(self):
+ raise ValueError("not a bool!")
+ masked_obj = np.ma.masked_array([NotBool(), 'b'], mask=[True, False])
+ # Check that the NotBool actually fails like we would expect:
+ with pytest.raises(ValueError, match="not a bool!"):
+ np.asarray([masked_obj], dtype=bool)
+
+ res = np.ma.asarray([masked_obj, normal_int])
+ assert_array_equal(res.mask, [[True, False], [False, False]])
+
def test_creation_from_ndarray_with_padding(self):
x = np.array([('A', 0)], dtype={'names':['f0','f1'],
'formats':['S4','i8'],