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)
| 7928 | |
| 7929 | |
| 7930 | def where(condition, x=_NoValue, y=_NoValue): |
| 7931 | """ |
| 7932 | Return a masked array with elements from `x` or `y`, depending on condition. |
| 7933 | |
| 7934 | .. note:: |
| 7935 | When only `condition` is provided, this function is identical to |
| 7936 | `nonzero`. The rest of this documentation covers only the case where |
| 7937 | all three arguments are provided. |
| 7938 | |
| 7939 | Parameters |
| 7940 | ---------- |
| 7941 | condition : array_like, bool |
| 7942 | Where True, yield `x`, otherwise yield `y`. |
| 7943 | x, y : array_like, optional |
| 7944 | Values from which to choose. `x`, `y` and `condition` need to be |
| 7945 | broadcastable to some shape. |
| 7946 | |
| 7947 | Returns |
| 7948 | ------- |
| 7949 | out : MaskedArray |
| 7950 | A masked array with `masked` elements where the condition is masked, |
| 7951 | elements from `x` where `condition` is True, and elements from `y` |
| 7952 | elsewhere. |
| 7953 | |
| 7954 | See Also |
| 7955 | -------- |
| 7956 | numpy.where : Equivalent function in the top-level NumPy module. |
| 7957 | nonzero : The function that is called when x and y are omitted |
| 7958 | |
| 7959 | Examples |
| 7960 | -------- |
| 7961 | >>> import numpy as np |
| 7962 | >>> x = np.ma.array(np.arange(9.).reshape(3, 3), mask=[[0, 1, 0], |
| 7963 | ... [1, 0, 1], |
| 7964 | ... [0, 1, 0]]) |
| 7965 | >>> x |
| 7966 | masked_array( |
| 7967 | data=[[0.0, --, 2.0], |
| 7968 | [--, 4.0, --], |
| 7969 | [6.0, --, 8.0]], |
| 7970 | mask=[[False, True, False], |
| 7971 | [ True, False, True], |
| 7972 | [False, True, False]], |
| 7973 | fill_value=1e+20) |
| 7974 | >>> np.ma.where(x > 5, x, -3.1416) |
| 7975 | masked_array( |
| 7976 | data=[[-3.1416, --, -3.1416], |
| 7977 | [--, -3.1416, --], |
| 7978 | [6.0, --, 8.0]], |
| 7979 | mask=[[False, True, False], |
| 7980 | [ True, False, True], |
| 7981 | [False, True, False]], |
| 7982 | fill_value=1e+20) |
| 7983 | |
| 7984 | """ |
| 7985 | |
| 7986 | # handle the single-argument case |
| 7987 | missing = (x is _NoValue, y is _NoValue).count(True) |
searching dependent graphs…