diff options
author | B R S Recht <brsr@users.noreply.github.com> | 2017-05-04 20:03:09 -0400 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2017-05-05 01:03:09 +0100 |
commit | 69b0c42bca27dd5d5522de306bcd7db7deccbfad (patch) | |
tree | b857fc11775a3633bf959a158f5d6be3e7ef7971 /numpy/lib/tests/test_arraysetops.py | |
parent | 1d592c12ca7f9c7f471aa8d20b538c5cb4f2cdce (diff) | |
download | numpy-69b0c42bca27dd5d5522de306bcd7db7deccbfad.tar.gz |
ENH: Add isin, genereralizing in1d to ND arrays (#8423)
This fixes gh-8331
Also update the docs for arraysetops to remove the outdated "1D" from the
description, which was already incorrect for np.unique.
Diffstat (limited to 'numpy/lib/tests/test_arraysetops.py')
-rw-r--r-- | numpy/lib/tests/test_arraysetops.py | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/numpy/lib/tests/test_arraysetops.py b/numpy/lib/tests/test_arraysetops.py index eb4cca0ce..fa664ff24 100644 --- a/numpy/lib/tests/test_arraysetops.py +++ b/numpy/lib/tests/test_arraysetops.py @@ -8,7 +8,7 @@ from numpy.testing import ( run_module_suite, TestCase, assert_array_equal, assert_equal, assert_raises ) from numpy.lib.arraysetops import ( - ediff1d, intersect1d, setxor1d, union1d, setdiff1d, unique, in1d + ediff1d, intersect1d, setxor1d, union1d, setdiff1d, unique, in1d, isin ) @@ -77,6 +77,46 @@ class TestSetOps(TestCase): assert(isinstance(ediff1d(np.matrix(1)), np.matrix)) assert(isinstance(ediff1d(np.matrix(1), to_begin=1), np.matrix)) + def test_isin(self): + # the tests for in1d cover most of isin's behavior + # if in1d is removed, would need to change those tests to test + # isin instead. + def _isin_slow(a, b): + b = np.asarray(b).flatten().tolist() + return a in b + isin_slow = np.vectorize(_isin_slow, otypes=[bool], excluded={1}) + def assert_isin_equal(a, b): + x = isin(a, b) + y = isin_slow(a, b) + assert_array_equal(x, y) + + #multidimensional arrays in both arguments + a = np.arange(24).reshape([2, 3, 4]) + b = np.array([[10, 20, 30], [0, 1, 3], [11, 22, 33]]) + assert_isin_equal(a, b) + + #array-likes as both arguments + c = [(9, 8), (7, 6)] + d = (9, 7) + assert_isin_equal(c, d) + + #zero-d array: + f = np.array(3) + assert_isin_equal(f, b) + assert_isin_equal(a, f) + assert_isin_equal(f, f) + + #scalar: + assert_isin_equal(5, b) + assert_isin_equal(a, 6) + assert_isin_equal(5, 6) + + #empty array-like: + x = [] + assert_isin_equal(x, b) + assert_isin_equal(a, x) + assert_isin_equal(x, x) + def test_in1d(self): # we use two different sizes for the b array here to test the # two different paths in in1d(). |