Fake a 'void' object to use for masked array with structured dtypes.
| 6343 | |
| 6344 | |
| 6345 | class mvoid(MaskedArray): |
| 6346 | """ |
| 6347 | Fake a 'void' object to use for masked array with structured dtypes. |
| 6348 | """ |
| 6349 | |
| 6350 | def __new__(self, data, mask=nomask, dtype=None, fill_value=None, |
| 6351 | hardmask=False, copy=False, subok=True): |
| 6352 | _data = np.array(data, copy=copy, subok=subok, dtype=dtype) |
| 6353 | _data = _data.view(self) |
| 6354 | _data._hardmask = hardmask |
| 6355 | if mask is not nomask: |
| 6356 | if isinstance(mask, np.void): |
| 6357 | _data._mask = mask |
| 6358 | else: |
| 6359 | try: |
| 6360 | # Mask is already a 0D array |
| 6361 | _data._mask = np.void(mask) |
| 6362 | except TypeError: |
| 6363 | # Transform the mask to a void |
| 6364 | mdtype = make_mask_descr(dtype) |
| 6365 | _data._mask = np.array(mask, dtype=mdtype)[()] |
| 6366 | if fill_value is not None: |
| 6367 | _data.fill_value = fill_value |
| 6368 | return _data |
| 6369 | |
| 6370 | @property |
| 6371 | def _data(self): |
| 6372 | # Make sure that the _data part is a np.void |
| 6373 | return super()._data[()] |
| 6374 | |
| 6375 | def __getitem__(self, indx): |
| 6376 | """ |
| 6377 | Get the index. |
| 6378 | |
| 6379 | """ |
| 6380 | m = self._mask |
| 6381 | if isinstance(m[indx], ndarray): |
| 6382 | # Can happen when indx is a multi-dimensional field: |
| 6383 | # A = ma.masked_array(data=[([0,1],)], mask=[([True, |
| 6384 | # False],)], dtype=[("A", ">i2", (2,))]) |
| 6385 | # x = A[0]; y = x["A"]; then y.mask["A"].size==2 |
| 6386 | # and we can not say masked/unmasked. |
| 6387 | # The result is no longer mvoid! |
| 6388 | # See also issue #6724. |
| 6389 | return masked_array( |
| 6390 | data=self._data[indx], mask=m[indx], |
| 6391 | fill_value=self._fill_value[indx], |
| 6392 | hard_mask=self._hardmask) |
| 6393 | if m is not nomask and m[indx]: |
| 6394 | return masked |
| 6395 | return self._data[indx] |
| 6396 | |
| 6397 | def __setitem__(self, indx, value): |
| 6398 | self._data[indx] = value |
| 6399 | if self._hardmask: |
| 6400 | self._mask[indx] |= getattr(value, "_mask", False) |
| 6401 | else: |
| 6402 | self._mask[indx] = getattr(value, "_mask", False) |
no outgoing calls