Changes elements of an array based on conditional and input values. This is the masked array version of `numpy.putmask`, for details see `numpy.putmask`. See Also -------- numpy.putmask Notes ----- Using a masked array as `values` will **not** transform a `nda
(a, mask, values)
| 7550 | |
| 7551 | |
| 7552 | def putmask(a, mask, values): # , mode='raise'): |
| 7553 | """ |
| 7554 | Changes elements of an array based on conditional and input values. |
| 7555 | |
| 7556 | This is the masked array version of `numpy.putmask`, for details see |
| 7557 | `numpy.putmask`. |
| 7558 | |
| 7559 | See Also |
| 7560 | -------- |
| 7561 | numpy.putmask |
| 7562 | |
| 7563 | Notes |
| 7564 | ----- |
| 7565 | Using a masked array as `values` will **not** transform a `ndarray` into |
| 7566 | a `MaskedArray`. |
| 7567 | |
| 7568 | Examples |
| 7569 | -------- |
| 7570 | >>> import numpy as np |
| 7571 | >>> arr = [[1, 2], [3, 4]] |
| 7572 | >>> mask = [[1, 0], [0, 0]] |
| 7573 | >>> x = np.ma.array(arr, mask=mask) |
| 7574 | >>> np.ma.putmask(x, x < 4, 10*x) |
| 7575 | >>> x |
| 7576 | masked_array( |
| 7577 | data=[[--, 20], |
| 7578 | [30, 4]], |
| 7579 | mask=[[ True, False], |
| 7580 | [False, False]], |
| 7581 | fill_value=999999) |
| 7582 | >>> x.data |
| 7583 | array([[10, 20], |
| 7584 | [30, 4]]) |
| 7585 | |
| 7586 | """ |
| 7587 | # We can't use 'frommethod', the order of arguments is different |
| 7588 | if not isinstance(a, MaskedArray): |
| 7589 | a = a.view(MaskedArray) |
| 7590 | (valdata, valmask) = (getdata(values), getmask(values)) |
| 7591 | if getmask(a) is nomask: |
| 7592 | if valmask is not nomask: |
| 7593 | a._sharedmask = True |
| 7594 | a._mask = make_mask_none(a.shape, a.dtype) |
| 7595 | np.copyto(a._mask, valmask, where=mask) |
| 7596 | elif a._hardmask: |
| 7597 | if valmask is not nomask: |
| 7598 | m = a._mask.copy() |
| 7599 | np.copyto(m, valmask, where=mask) |
| 7600 | a.mask |= m |
| 7601 | else: |
| 7602 | if valmask is nomask: |
| 7603 | valmask = getmaskarray(values) |
| 7604 | np.copyto(a._mask, valmask, where=mask) |
| 7605 | np.copyto(a._data, valdata, where=mask) |
| 7606 | |
| 7607 | |
| 7608 | def transpose(a, axes=None): |
searching dependent graphs…