x.__getitem__(y) <==> x[y] Return the item described by i, as a masked array.
(self, indx)
| 3215 | return output |
| 3216 | |
| 3217 | def __getitem__(self, indx): |
| 3218 | """ |
| 3219 | x.__getitem__(y) <==> x[y] |
| 3220 | |
| 3221 | Return the item described by i, as a masked array. |
| 3222 | |
| 3223 | """ |
| 3224 | # We could directly use ndarray.__getitem__ on self. |
| 3225 | # But then we would have to modify __array_finalize__ to prevent the |
| 3226 | # mask of being reshaped if it hasn't been set up properly yet |
| 3227 | # So it's easier to stick to the current version |
| 3228 | dout = self.data[indx] |
| 3229 | _mask = self._mask |
| 3230 | |
| 3231 | def _is_scalar(m): |
| 3232 | return not isinstance(m, np.ndarray) |
| 3233 | |
| 3234 | def _scalar_heuristic(arr, elem): |
| 3235 | """ |
| 3236 | Return whether `elem` is a scalar result of indexing `arr`, or None |
| 3237 | if undecidable without promoting nomask to a full mask |
| 3238 | """ |
| 3239 | # obviously a scalar |
| 3240 | if not isinstance(elem, np.ndarray): |
| 3241 | return True |
| 3242 | |
| 3243 | # object array scalar indexing can return anything |
| 3244 | elif arr.dtype.type is np.object_: |
| 3245 | if arr.dtype is not elem.dtype: |
| 3246 | # elem is an array, but dtypes do not match, so must be |
| 3247 | # an element |
| 3248 | return True |
| 3249 | |
| 3250 | # well-behaved subclass that only returns 0d arrays when |
| 3251 | # expected - this is not a scalar |
| 3252 | elif type(arr).__getitem__ == ndarray.__getitem__: |
| 3253 | return False |
| 3254 | |
| 3255 | return None |
| 3256 | |
| 3257 | if _mask is not nomask: |
| 3258 | # _mask cannot be a subclass, so it tells us whether we should |
| 3259 | # expect a scalar. It also cannot be of dtype object. |
| 3260 | mout = _mask[indx] |
| 3261 | scalar_expected = _is_scalar(mout) |
| 3262 | |
| 3263 | else: |
| 3264 | # attempt to apply the heuristic to avoid constructing a full mask |
| 3265 | mout = nomask |
| 3266 | scalar_expected = _scalar_heuristic(self.data, dout) |
| 3267 | if scalar_expected is None: |
| 3268 | # heuristics have failed |
| 3269 | # construct a full array, so we can be certain. This is costly. |
| 3270 | # we could also fall back on ndarray.__getitem__(self.data, indx) |
| 3271 | scalar_expected = _is_scalar(getmaskarray(self)[indx]) |
| 3272 | |
| 3273 | # Did we extract a single item? |
| 3274 | if scalar_expected: |
nothing calls this directly
no test coverage detected