Return a masked array with elements from `x` or `y`, depending on condition. .. note:: When only `condition` is provided, this function is identical to `nonzero`. The rest of this documentation covers only the case where all three arguments are provided. Parame
(condition, x=_NoValue, y=_NoValue)
| 7601 | |
| 7602 | |
| 7603 | def where(condition, x=_NoValue, y=_NoValue): |
| 7604 | """ |
| 7605 | Return a masked array with elements from `x` or `y`, depending on condition. |
| 7606 | |
| 7607 | .. note:: |
| 7608 | When only `condition` is provided, this function is identical to |
| 7609 | `nonzero`. The rest of this documentation covers only the case where |
| 7610 | all three arguments are provided. |
| 7611 | |
| 7612 | Parameters |
| 7613 | ---------- |
| 7614 | condition : array_like, bool |
| 7615 | Where True, yield `x`, otherwise yield `y`. |
| 7616 | x, y : array_like, optional |
| 7617 | Values from which to choose. `x`, `y` and `condition` need to be |
| 7618 | broadcastable to some shape. |
| 7619 | |
| 7620 | Returns |
| 7621 | ------- |
| 7622 | out : MaskedArray |
| 7623 | An masked array with `masked` elements where the condition is masked, |
| 7624 | elements from `x` where `condition` is True, and elements from `y` |
| 7625 | elsewhere. |
| 7626 | |
| 7627 | See Also |
| 7628 | -------- |
| 7629 | numpy.where : Equivalent function in the top-level NumPy module. |
| 7630 | nonzero : The function that is called when x and y are omitted |
| 7631 | |
| 7632 | Examples |
| 7633 | -------- |
| 7634 | >>> x = np.ma.array(np.arange(9.).reshape(3, 3), mask=[[0, 1, 0], |
| 7635 | ... [1, 0, 1], |
| 7636 | ... [0, 1, 0]]) |
| 7637 | >>> x |
| 7638 | masked_array( |
| 7639 | data=[[0.0, --, 2.0], |
| 7640 | [--, 4.0, --], |
| 7641 | [6.0, --, 8.0]], |
| 7642 | mask=[[False, True, False], |
| 7643 | [ True, False, True], |
| 7644 | [False, True, False]], |
| 7645 | fill_value=1e+20) |
| 7646 | >>> np.ma.where(x > 5, x, -3.1416) |
| 7647 | masked_array( |
| 7648 | data=[[-3.1416, --, -3.1416], |
| 7649 | [--, -3.1416, --], |
| 7650 | [6.0, --, 8.0]], |
| 7651 | mask=[[False, True, False], |
| 7652 | [ True, False, True], |
| 7653 | [False, True, False]], |
| 7654 | fill_value=1e+20) |
| 7655 | |
| 7656 | """ |
| 7657 | |
| 7658 | # handle the single-argument case |
| 7659 | missing = (x is _NoValue, y is _NoValue).count(True) |
| 7660 | if missing == 1: |