Skip to content

BUG: .isin(...) now coerces sets to lists #13014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def isin(comps, values):
raise TypeError("only list-like objects are allowed to be passed"
" to isin(), you passed a "
"[{0}]".format(type(values).__name__))
if com.is_set_like(values):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of doing this, I think you can:

if not isinstance(values, np.ndarray):
    values = list(values)

Then don't even need the next check

values = list(values)

# GH11232
# work-around for numpy < 1.8 and comparisions on py3
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1765,6 +1765,10 @@ def is_list_like(arg):
not isinstance(arg, compat.string_and_binary_types))


def is_set_like(arg):
return (is_list_like(arg) and not hasattr(arg, '__getitem__'))


def is_dict_like(arg):
return hasattr(arg, '__getitem__') and hasattr(arg, 'keys')

Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,9 @@ def test_isin_with_i8(self):
result = s.isin([np.datetime64(s[1])])
assert_series_equal(result, expected2)

result = s.isin(set(s[0:2]))
assert_series_equal(result, expected)

# timedelta64[ns]
s = Series(pd.to_timedelta(lrange(5), unit='d'))
result = s.isin(s[0:2])
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,18 @@ def test_is_list_like():
assert not com.is_list_like(f)


def test_is_set_like():
passes = (set([1, 'a']), frozenset([1, 'a']))
fails = ([], [1], (1, ), (1, 2), {'a': 1}, Series([1]), Series([]),
Series(['a']).str, 1, '2', object())

for p in passes:
assert com.is_set_like(p)

for f in fails:
assert not com.is_set_like(f)


def test_is_dict_like():
passes = [{}, {'A': 1}, pd.Series([1])]
fails = ['1', 1, [1, 2], (1, 2), range(2), pd.Index([1])]
Expand Down