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/ma | |
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/ma')
-rw-r--r-- | numpy/ma/extras.py | 29 | ||||
-rw-r--r-- | numpy/ma/tests/test_extras.py | 23 |
2 files changed, 50 insertions, 2 deletions
diff --git a/numpy/ma/extras.py b/numpy/ma/extras.py index 4955d25eb..e100e471c 100644 --- a/numpy/ma/extras.py +++ b/numpy/ma/extras.py @@ -16,7 +16,7 @@ __all__ = [ 'column_stack', 'compress_cols', 'compress_nd', 'compress_rowcols', 'compress_rows', 'count_masked', 'corrcoef', 'cov', 'diagflat', 'dot', 'dstack', 'ediff1d', 'flatnotmasked_contiguous', 'flatnotmasked_edges', - 'hsplit', 'hstack', 'in1d', 'intersect1d', 'mask_cols', 'mask_rowcols', + 'hsplit', 'hstack', 'isin', 'in1d', 'intersect1d', 'mask_cols', 'mask_rowcols', 'mask_rows', 'masked_all', 'masked_all_like', 'median', 'mr_', 'notmasked_contiguous', 'notmasked_edges', 'polyfit', 'row_stack', 'setdiff1d', 'setxor1d', 'unique', 'union1d', 'vander', 'vstack', @@ -1131,6 +1131,7 @@ def setxor1d(ar1, ar2, assume_unique=False): flag2 = (flag[1:] == flag[:-1]) return aux[flag2] + def in1d(ar1, ar2, assume_unique=False, invert=False): """ Test whether each element of an array is also present in a second @@ -1138,8 +1139,11 @@ def in1d(ar1, ar2, assume_unique=False, invert=False): The output is always a masked array. See `numpy.in1d` for more details. + We recommend using :func:`isin` instead of `in1d` for new code. + See Also -------- + isin : Version of this function that preserves the shape of ar1. numpy.in1d : Equivalent function for ndarrays. Notes @@ -1170,6 +1174,29 @@ def in1d(ar1, ar2, assume_unique=False, invert=False): return flag[indx][rev_idx] +def isin(element, test_elements, assume_unique=False, invert=False): + """ + Calculates `element in test_elements`, broadcasting over + `element` only. + + The output is always a masked array of the same shape as `element`. + See `numpy.isin` for more details. + + See Also + -------- + in1d : Flattened version of this function. + numpy.isin : Equivalent function for ndarrays. + + Notes + ----- + .. versionadded:: 1.13.0 + + """ + element = ma.asarray(element) + return in1d(element, test_elements, assume_unique=assume_unique, + invert=invert).reshape(element.shape) + + def union1d(ar1, ar2): """ Union of two arrays. diff --git a/numpy/ma/tests/test_extras.py b/numpy/ma/tests/test_extras.py index 77a5c0fc6..e7ebd8b82 100644 --- a/numpy/ma/tests/test_extras.py +++ b/numpy/ma/tests/test_extras.py @@ -28,7 +28,7 @@ from numpy.ma.extras import ( median, average, unique, setxor1d, setdiff1d, union1d, intersect1d, in1d, ediff1d, apply_over_axes, apply_along_axis, compress_nd, compress_rowcols, mask_rowcols, clump_masked, clump_unmasked, flatnotmasked_contiguous, - notmasked_contiguous, notmasked_edges, masked_all, masked_all_like, + notmasked_contiguous, notmasked_edges, masked_all, masked_all_like, isin, diagflat ) import numpy.ma.extras as mae @@ -1435,6 +1435,27 @@ class TestArraySetOps(TestCase): # assert_array_equal([], setxor1d([], [])) + 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. + a = np.arange(24).reshape([2, 3, 4]) + mask = np.zeros([2, 3, 4]) + mask[1, 2, 0] = 1 + a = array(a, mask=mask) + b = array(data=[0, 10, 20, 30, 1, 3, 11, 22, 33], + mask=[0, 1, 0, 1, 0, 1, 0, 1, 0]) + ec = zeros((2, 3, 4), dtype=bool) + ec[0, 0, 0] = True + ec[0, 0, 1] = True + ec[0, 2, 3] = True + c = isin(a, b) + assert_(isinstance(c, MaskedArray)) + assert_array_equal(c, ec) + #compare results of np.isin to ma.isin + d = np.isin(a, b[~b.mask]) & ~a.mask + assert_array_equal(c, d) + def test_in1d(self): # Test in1d a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1]) |