x.__getitem__(y) <==> x[y] Return the item described by i, as a masked array.
(self, indx)
| 3275 | return output |
| 3276 | |
| 3277 | def __getitem__(self, indx): |
| 3278 | """ |
| 3279 | x.__getitem__(y) <==> x[y] |
| 3280 | |
| 3281 | Return the item described by i, as a masked array. |
| 3282 | |
| 3283 | """ |
| 3284 | # We could directly use ndarray.__getitem__ on self. |
| 3285 | # But then we would have to modify __array_finalize__ to prevent the |
| 3286 | # mask of being reshaped if it hasn't been set up properly yet |
| 3287 | # So it's easier to stick to the current version |
| 3288 | dout = self.data[indx] |
| 3289 | _mask = self._mask |
| 3290 | |
| 3291 | def _is_scalar(m): |
| 3292 | return not isinstance(m, np.ndarray) |
| 3293 | |
| 3294 | def _scalar_heuristic(arr, elem): |
| 3295 | """ |
| 3296 | Return whether `elem` is a scalar result of indexing `arr`, or None |
| 3297 | if undecidable without promoting nomask to a full mask |
| 3298 | """ |
| 3299 | # obviously a scalar |
| 3300 | if not isinstance(elem, np.ndarray): |
| 3301 | return True |
| 3302 | |
| 3303 | # object array scalar indexing can return anything |
| 3304 | elif arr.dtype.type is np.object_: |
| 3305 | if arr.dtype is not elem.dtype: |
| 3306 | # elem is an array, but dtypes do not match, so must be |
| 3307 | # an element |
| 3308 | return True |
| 3309 | |
| 3310 | # well-behaved subclass that only returns 0d arrays when |
| 3311 | # expected - this is not a scalar |
| 3312 | elif type(arr).__getitem__ == ndarray.__getitem__: |
| 3313 | return False |
| 3314 | |
| 3315 | return None |
| 3316 | |
| 3317 | if _mask is not nomask: |
| 3318 | # _mask cannot be a subclass, so it tells us whether we should |
| 3319 | # expect a scalar. It also cannot be of dtype object. |
| 3320 | mout = _mask[indx] |
| 3321 | scalar_expected = _is_scalar(mout) |
| 3322 | |
| 3323 | else: |
| 3324 | # attempt to apply the heuristic to avoid constructing a full mask |
| 3325 | mout = nomask |
| 3326 | scalar_expected = _scalar_heuristic(self.data, dout) |
| 3327 | if scalar_expected is None: |
| 3328 | # heuristics have failed |
| 3329 | # construct a full array, so we can be certain. This is costly. |
| 3330 | # we could also fall back on ndarray.__getitem__(self.data, indx) |
| 3331 | scalar_expected = _is_scalar(getmaskarray(self)[indx]) |
| 3332 | |
| 3333 | # Did we extract a single item? |
| 3334 | if scalar_expected: |
nothing calls this directly
no test coverage detected