diff options
| author | Richard Hattersley <rhattersley@gmail.com> | 2012-11-22 16:02:05 +0000 |
|---|---|---|
| committer | Charles Harris <charlesr.harris@gmail.com> | 2013-08-17 12:38:30 -0600 |
| commit | 0cc113c7574e40172fe4a48e9c0d90477d736ce3 (patch) | |
| tree | 757c505a424905d379a92bc7077356664efd6f8b | |
| parent | aeb13affe23abaa9550521531b61f03850551106 (diff) | |
| download | numpy-0cc113c7574e40172fe4a48e9c0d90477d736ce3.tar.gz | |
MAINT: Make setting mask values with scalars faster.
The current implementation assigns the mask value to a.flat, where a is
the mask being updated. In the scalar case, that is slower than
assigning to a[...], so when a is an instance of int, float, np.bool_,
or np.number use that instead.
| -rw-r--r-- | numpy/ma/core.py | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index ccf62bdcf..8f2dbedeb 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -3129,6 +3129,11 @@ class MaskedArray(ndarray): if self._hardmask: current_mask |= mask # Softmask: set everything to False + # If it's obviously a compatible scalar, use a quick update + # method... + elif isinstance(mask, (int, float, np.bool_, np.number)): + current_mask[...] = mask + # ...otherwise fall back to the slower, general purpose way. else: current_mask.flat = mask # Named fields w/ ............ @@ -3158,6 +3163,11 @@ class MaskedArray(ndarray): for n in idtype.names: current_mask[n] |= mask[n] # Softmask: set everything to False + # If it's obviously a compatible scalar, use a quick update + # method... + elif isinstance(mask, (int, float, np.bool_, np.number)): + current_mask[...] = mask + # ...otherwise fall back to the slower, general purpose way. else: current_mask.flat = mask # Reshape if needed |
