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

Method test_where

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

Source from the content-addressed store, hash-verified

4482
4483 @pytest.mark.skipif(IS_WASM, reason="fp errors don't work in wasm")
4484 def test_where(self):
4485 # Test the where function
4486 x = np.array([1., 1., 1., -2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.])
4487 y = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.])
4488 m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
4489 m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1]
4490 xm = masked_array(x, mask=m1)
4491 ym = masked_array(y, mask=m2)
4492 xm.set_fill_value(1e+20)
4493
4494 d = where(xm > 2, xm, -9)
4495 assert_equal(d, [-9., -9., -9., -9., -9., 4.,
4496 -9., -9., 10., -9., -9., 3.])
4497 assert_equal(d._mask, xm._mask)
4498 d = where(xm > 2, -9, ym)
4499 assert_equal(d, [5., 0., 3., 2., -1., -9.,
4500 -9., -10., -9., 1., 0., -9.])
4501 assert_equal(d._mask, [1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0])
4502 d = where(xm > 2, xm, masked)
4503 assert_equal(d, [-9., -9., -9., -9., -9., 4.,
4504 -9., -9., 10., -9., -9., 3.])
4505 tmp = xm._mask.copy()
4506 tmp[(xm <= 2).filled(True)] = True
4507 assert_equal(d._mask, tmp)
4508
4509 with np.errstate(invalid="warn"):
4510 # The fill value is 1e20, it cannot be converted to `int`:
4511 with pytest.warns(RuntimeWarning, match="invalid value"):
4512 ixm = xm.astype(int)
4513 d = where(ixm > 2, ixm, masked)
4514 assert_equal(d, [-9, -9, -9, -9, -9, 4, -9, -9, 10, -9, -9, 3])
4515 assert_equal(d.dtype, ixm.dtype)
4516
4517 def test_where_object(self):
4518 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