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

Function masked_where

numpy/ma/core.py:1828–1947  ·  view source on GitHub ↗

Mask an array where a condition is met. Return `a` as an array masked where `condition` is True. Any masked values of `a` or `condition` are also masked in the output. Parameters ---------- condition : array_like Masking condition. When `condition` tests floating

(condition, a, copy=True)

Source from the content-addressed store, hash-verified

1826###############################################################################
1827
1828def masked_where(condition, a, copy=True):
1829 """
1830 Mask an array where a condition is met.
1831
1832 Return `a` as an array masked where `condition` is True.
1833 Any masked values of `a` or `condition` are also masked in the output.
1834
1835 Parameters
1836 ----------
1837 condition : array_like
1838 Masking condition. When `condition` tests floating point values for
1839 equality, consider using ``masked_values`` instead.
1840 a : array_like
1841 Array to mask.
1842 copy : bool
1843 If True (default) make a copy of `a` in the result. If False modify
1844 `a` in place and return a view.
1845
1846 Returns
1847 -------
1848 result : MaskedArray
1849 The result of masking `a` where `condition` is True.
1850
1851 See Also
1852 --------
1853 masked_values : Mask using floating point equality.
1854 masked_equal : Mask where equal to a given value.
1855 masked_not_equal : Mask where `not` equal to a given value.
1856 masked_less_equal : Mask where less than or equal to a given value.
1857 masked_greater_equal : Mask where greater than or equal to a given value.
1858 masked_less : Mask where less than a given value.
1859 masked_greater : Mask where greater than a given value.
1860 masked_inside : Mask inside a given interval.
1861 masked_outside : Mask outside a given interval.
1862 masked_invalid : Mask invalid values (NaNs or infs).
1863
1864 Examples
1865 --------
1866 >>> import numpy.ma as ma
1867 >>> a = np.arange(4)
1868 >>> a
1869 array([0, 1, 2, 3])
1870 >>> ma.masked_where(a <= 2, a)
1871 masked_array(data=[--, --, --, 3],
1872 mask=[ True, True, True, False],
1873 fill_value=999999)
1874
1875 Mask array `b` conditional on `a`.
1876
1877 >>> b = ['a', 'b', 'c', 'd']
1878 >>> ma.masked_where(a == 2, b)
1879 masked_array(data=['a', 'b', --, 'd'],
1880 mask=[False, False, True, False],
1881 fill_value='N/A',
1882 dtype='<U1')
1883
1884 Effect of the `copy` argument.
1885

Callers 15

test_mem_masked_whereMethod · 0.90
test_testOddFeaturesMethod · 0.90
test_minmaxMethod · 0.90
test_oddfeatures_1Method · 0.90
test_minmaxMethod · 0.90
masked_greaterFunction · 0.85
masked_greater_equalFunction · 0.85
masked_lessFunction · 0.85

Calls 5

make_maskFunction · 0.85
mask_orFunction · 0.85
_shrink_maskFunction · 0.85
getmaskFunction · 0.85
viewMethod · 0.45

Tested by 9

test_mem_masked_whereMethod · 0.72
test_testOddFeaturesMethod · 0.72
test_minmaxMethod · 0.72
test_oddfeatures_1Method · 0.72
test_minmaxMethod · 0.72