Skip to content

Commit 78cd18e

Browse files
committed
BUG pandas-dev#16012 - fix isin for large object arrays
1 parent 4642a2d commit 78cd18e

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,4 @@ Categorical
200200
Other
201201
^^^^^
202202
- Bug in :func:`eval` where the ``inplace`` parameter was being incorrectly handled (:issue:`16732`)
203+
- Bug when using :func:`isin` on a large object series and large comparison array, numpy's in1d is used but doesn't support objects in most conditions (:issue:`16012`)

pandas/core/algorithms.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,10 @@ def isin(comps, values):
402402
# work-around for numpy < 1.8 and comparisions on py3
403403
# faster for larger cases to use np.in1d
404404
f = lambda x, y: htable.ismember_object(x, values)
405-
if (_np_version_under1p8 and compat.PY3) or len(comps) > 1000000:
405+
# GH16012
406+
# Ensure np.in1d doesn't get object types or it *may* throw an exception
407+
if ((_np_version_under1p8 and compat.PY3) or len(comps) > 1000000 and
408+
not is_object_dtype(comps)):
406409
f = lambda x, y: np.in1d(x, y)
407410
elif is_integer_dtype(comps):
408411
try:

pandas/tests/series/test_analytics.py

+9
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,15 @@ def test_isin(self):
10921092
expected = Series([True, False, True, False, False, False, True, True])
10931093
assert_series_equal(result, expected)
10941094

1095+
# GH: 16012
1096+
# This specific issue has to have a series over 1e6 in len, but the
1097+
# comparison array (in_list) must be large enough so that numpy doesn't
1098+
# do a manual masking trick that will avoid this issue altogether
1099+
s = Series(list('abcdefghijk' * 10 ** 5))
1100+
in_list = [-1, 'a', 'b', 'G', 'Y', 'Z', 'E', 'K', 'E', 'S', 'I', 'R', 'R']*6
1101+
1102+
assert s.isin(in_list).sum() == 200000
1103+
10951104
def test_isin_with_string_scalar(self):
10961105
# GH4763
10971106
s = Series(['A', 'B', 'C', 'a', 'B', 'B', 'A', 'C'])

0 commit comments

Comments
 (0)