MCPcopy Create free account
hub / github.com/numpy/numpy / where

Function where

numpy/ma/core.py:7603–7691  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

7601
7602
7603def 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:

Callers 15

test_testOddFeaturesMethod · 0.90
test_testMinMax2Method · 0.90
test_oddfeatures_1Method · 0.90
test_oddfeatures_2Method · 0.90
test_minmax_funcMethod · 0.90
test_roundMethod · 0.90
test_whereMethod · 0.90
test_where_typeMethod · 0.90
test_where_broadcastMethod · 0.90

Calls 6

filledFunction · 0.85
getdataFunction · 0.85
getmaskarrayFunction · 0.85
_shrink_maskFunction · 0.85
nonzeroFunction · 0.50
countMethod · 0.45

Tested by 12

test_testOddFeaturesMethod · 0.72
test_testMinMax2Method · 0.72
test_oddfeatures_1Method · 0.72
test_oddfeatures_2Method · 0.72
test_minmax_funcMethod · 0.72
test_roundMethod · 0.72
test_whereMethod · 0.72
test_where_typeMethod · 0.72
test_where_broadcastMethod · 0.72