Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42e5b80

Browse files
sebergcertik
authored andcommitted
BUG: Fix regression for in1d with non-array input
There was a regression introduced by the speed improvement in commit 6441c2a. This fixes it, and generally ravels the arrays for np.in1d. However it can be argued that at least the first array should not be ravelled in the future. Fixes "Issue gh-2755"
1 parent 60df7b0 commit 42e5b80

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

numpy/lib/arraysetops.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ def in1d(ar1, ar2, assume_unique=False):
324324
array([0, 2, 0])
325325
326326
"""
327+
# Ravel both arrays, behavior for the first array could be different
328+
ar1 = np.asarray(ar1).ravel()
329+
ar2 = np.asarray(ar2).ravel()
330+
327331
# This code is significantly faster when the condition is satisfied.
328332
if len(ar2) < 10 * len(ar1) ** 0.145:
329333
mask = np.zeros(len(ar1), dtype=np.bool)

numpy/lib/tests/test_arraysetops.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,9 @@ def test_in1d(self):
124124
# we use two different sizes for the b array here to test the
125125
# two different paths in in1d().
126126
for mult in (1, 10):
127-
a = np.array([5, 7, 1, 2])
128-
b = np.array([2, 4, 3, 1, 5] * mult)
127+
# One check without np.array, to make sure lists are handled correct
128+
a = [5, 7, 1, 2]
129+
b = [2, 4, 3, 1, 5] * mult
129130
ec = np.array([True, False, True, True])
130131
c = in1d(a, b, assume_unique=True)
131132
assert_array_equal(c, ec)

0 commit comments

Comments
 (0)