MCPcopy Index your code
hub / github.com/numpy/numpy / test_where

Method test_where

numpy/ma/tests/test_core.py:4711–4742  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

4709
4710 @pytest.mark.skipif(IS_WASM, reason="fp errors don't work in wasm")
4711 def test_where(self):
4712 # Test the where function
4713 x = np.array([1., 1., 1., -2., pi / 2.0, 4., 5., -10., 10., 1., 2., 3.])
4714 y = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.])
4715 m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
4716 m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1]
4717 xm = masked_array(x, mask=m1)
4718 ym = masked_array(y, mask=m2)
4719 xm.set_fill_value(1e+20)
4720
4721 d = where(xm > 2, xm, -9)
4722 assert_equal(d, [-9., -9., -9., -9., -9., 4.,
4723 -9., -9., 10., -9., -9., 3.])
4724 assert_equal(d._mask, xm._mask)
4725 d = where(xm > 2, -9, ym)
4726 assert_equal(d, [5., 0., 3., 2., -1., -9.,
4727 -9., -10., -9., 1., 0., -9.])
4728 assert_equal(d._mask, [1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0])
4729 d = where(xm > 2, xm, masked)
4730 assert_equal(d, [-9., -9., -9., -9., -9., 4.,
4731 -9., -9., 10., -9., -9., 3.])
4732 tmp = xm._mask.copy()
4733 tmp[(xm <= 2).filled(True)] = True
4734 assert_equal(d._mask, tmp)
4735
4736 with np.errstate(invalid="warn"):
4737 # The fill value is 1e20, it cannot be converted to `int`:
4738 with pytest.warns(RuntimeWarning, match="invalid value"):
4739 ixm = xm.astype(int)
4740 d = where(ixm > 2, ixm, masked)
4741 assert_equal(d, [-9, -9, -9, -9, -9, 4, -9, -9, 10, -9, -9, 3])
4742 assert_equal(d.dtype, ixm.dtype)
4743
4744 def test_where_object(self):
4745 a = np.array(None)

Callers

nothing calls this directly

Calls 5

whereFunction · 0.90
assert_equalFunction · 0.90
astypeMethod · 0.80
copyMethod · 0.45
filledMethod · 0.45

Tested by

no test coverage detected