summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLefteris Loukas <eleftheriosloukas@gmail.com>2022-12-05 12:47:07 +0200
committerGitHub <noreply@github.com>2022-12-05 11:47:07 +0100
commitcfad62fd73c2a0f3d0dc4d73989071e331898a09 (patch)
treefd5eac05fc7616d1e2a00e61909b1c37937d6280
parentbc88c024edcc6dccc478748006e7650a61829331 (diff)
downloadnumpy-cfad62fd73c2a0f3d0dc4d73989071e331898a09.tar.gz
ENH: Speedup masked array creation when mask=None (#22725)
This PR Closes gh-17046. The problem was that when calling mask=None, the array creation took seconds compared to the microseconds needed when calling mask=False. Using `mask=None` is a bit dubious, since it has a different meaning from the default `mask=nomask`, but the speed trap is so hard to find, that it seems pragmatic to support it. OTOH, it also would seem fine to deprecate the whole path (or maybe see if the path can be sped up so that the speed difference isn't forbidding eough to bother).
-rw-r--r--numpy/ma/core.py8
-rw-r--r--numpy/ma/tests/test_core.py2
2 files changed, 9 insertions, 1 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py
index be213ebf3..59ba3a593 100644
--- a/numpy/ma/core.py
+++ b/numpy/ma/core.py
@@ -2835,7 +2835,6 @@ class MaskedArray(ndarray):
# Process mask.
# Type of the mask
mdtype = make_mask_descr(_data.dtype)
-
if mask is nomask:
# Case 1. : no mask in input.
# Erase the current mask ?
@@ -2870,6 +2869,12 @@ class MaskedArray(ndarray):
else:
# Case 2. : With a mask in input.
# If mask is boolean, create an array of True or False
+
+ # if users pass `mask=None` be forgiving here and cast it False
+ # for speed; although the default is `mask=nomask` and can differ.
+ if mask is None:
+ mask = False
+
if mask is True and mdtype == MaskType:
mask = np.ones(_data.shape, dtype=mdtype)
elif mask is False and mdtype == MaskType:
@@ -2917,6 +2922,7 @@ class MaskedArray(ndarray):
else:
_data._mask = np.logical_or(mask, _data._mask)
_data._sharedmask = False
+
# Update fill_value.
if fill_value is None:
fill_value = getattr(data, '_fill_value', None)
diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py
index 028f64c61..bc897d731 100644
--- a/numpy/ma/tests/test_core.py
+++ b/numpy/ma/tests/test_core.py
@@ -214,6 +214,8 @@ class TestMaskedArray:
assert_(np.may_share_memory(x.mask, y.mask))
y = array([1, 2, 3], mask=x._mask, copy=True)
assert_(not np.may_share_memory(x.mask, y.mask))
+ x = array([1, 2, 3], mask=None)
+ assert_equal(x._mask, [False, False, False])
def test_masked_singleton_array_creation_warns(self):
# The first works, but should not (ideally), there may be no way