Skip to content

Commit 05e734a

Browse files
Maoyuan Liujreback
Maoyuan Liu
authored andcommitted
BUG: .isin(...) now coerces sets to lists
closes pandas-dev#12988 closes pandas-dev#13014
1 parent 23eb483 commit 05e734a

File tree

8 files changed

+42
-4
lines changed

8 files changed

+42
-4
lines changed

doc/source/whatsnew/v0.18.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ Other Enhancements
238238

239239
- ``pd.crosstab()`` has gained a ``normalize`` argument for normalizing frequency tables (:issue:`12569`). Examples in the updated docs :ref:`here <reshaping.crosstabulations>`.
240240
- ``.resample(..).interpolate()`` is now supported (:issue:`12925`)
241+
- ``.isin()`` now accepts passed ``sets`` (:issue:`12988`)
241242

242243
.. _whatsnew_0181.sparse:
243244

@@ -584,7 +585,6 @@ Bug Fixes
584585

585586
- Bug in consistency of ``.name`` on ``.groupby(..).apply(..)`` cases (:issue:`12363`)
586587

587-
588588
- Bug in ``Timestamp.__repr__`` that caused ``pprint`` to fail in nested structures (:issue:`12622`)
589589
- Bug in ``Timedelta.min`` and ``Timedelta.max``, the properties now report the true minimum/maximum ``timedeltas`` as recognized by Pandas. See :ref:`documentation <timedeltas.limitations>`. (:issue:`12727`)
590590
- Bug in ``.quantile()`` with interpolation may coerce to ``float`` unexpectedly (:issue:`12772`)

pandas/core/algorithms.py

+2
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ def isin(comps, values):
114114
raise TypeError("only list-like objects are allowed to be passed"
115115
" to isin(), you passed a "
116116
"[{0}]".format(type(values).__name__))
117+
if not isinstance(values, np.ndarray):
118+
values = list(values)
117119

118120
# GH11232
119121
# work-around for numpy < 1.8 and comparisions on py3

pandas/core/series.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2370,11 +2370,15 @@ def isin(self, values):
23702370
23712371
Parameters
23722372
----------
2373-
values : list-like
2373+
values : set or list-like
23742374
The sequence of values to test. Passing in a single string will
23752375
raise a ``TypeError``. Instead, turn a single string into a
23762376
``list`` of one element.
23772377
2378+
.. versionadded:: 0.18.1
2379+
2380+
Support for values as a set
2381+
23782382
Returns
23792383
-------
23802384
isin : Series (bool dtype)

pandas/formats/format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ def write_result(self, buf):
977977
import IPython
978978
if IPython.__version__ < LooseVersion('3.0.0'):
979979
div_style = ' style="max-width:1500px;overflow:auto;"'
980-
except ImportError:
980+
except (ImportError, AttributeError):
981981
pass
982982

983983
self.write('<div{0}>'.format(div_style))

pandas/indexes/base.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2246,8 +2246,13 @@ def isin(self, values, level=None):
22462246
22472247
Parameters
22482248
----------
2249-
values : set or sequence of values
2249+
values : set or list-like
22502250
Sought values.
2251+
2252+
.. versionadded:: 0.18.1
2253+
2254+
Support for values as a set
2255+
22512256
level : str or int, optional
22522257
Name or position of the index level to use (if the index is a
22532258
MultiIndex).

pandas/tests/indexes/test_base.py

+4
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,10 @@ def test_isin(self):
11911191
expected = np.array([False, False, True, True])
11921192
tm.assert_numpy_array_equal(result, expected)
11931193

1194+
# set
1195+
result = idx.isin(set(values))
1196+
tm.assert_numpy_array_equal(result, expected)
1197+
11941198
# empty, return dtype bool
11951199
idx = Index([])
11961200
result = idx.isin(values)

pandas/tests/series/test_analytics.py

+3
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,9 @@ def test_isin_with_i8(self):
11401140
result = s.isin([np.datetime64(s[1])])
11411141
assert_series_equal(result, expected2)
11421142

1143+
result = s.isin(set(s[0:2]))
1144+
assert_series_equal(result, expected)
1145+
11431146
# timedelta64[ns]
11441147
s = Series(pd.to_timedelta(lrange(5), unit='d'))
11451148
result = s.isin(s[0:2])

pandas/tests/test_algos.py

+20
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ def test_basic(self):
345345
expected = np.array([True, False])
346346
tm.assert_numpy_array_equal(result, expected)
347347

348+
result = algos.isin(pd.Series([1, 2]), set([1]))
349+
expected = np.array([True, False])
350+
tm.assert_numpy_array_equal(result, expected)
351+
348352
result = algos.isin(['a', 'b'], ['a'])
349353
expected = np.array([True, False])
350354
tm.assert_numpy_array_equal(result, expected)
@@ -353,6 +357,10 @@ def test_basic(self):
353357
expected = np.array([True, False])
354358
tm.assert_numpy_array_equal(result, expected)
355359

360+
result = algos.isin(pd.Series(['a', 'b']), set(['a']))
361+
expected = np.array([True, False])
362+
tm.assert_numpy_array_equal(result, expected)
363+
356364
result = algos.isin(['a', 'b'], [1])
357365
expected = np.array([False, False])
358366
tm.assert_numpy_array_equal(result, expected)
@@ -366,11 +374,23 @@ def test_basic(self):
366374
expected = np.array([True, True, False])
367375
tm.assert_numpy_array_equal(result, expected)
368376

377+
result = algos.isin(arr, set(arr[0:2]))
378+
expected = np.array([True, True, False])
379+
tm.assert_numpy_array_equal(result, expected)
380+
369381
arr = pd.timedelta_range('1 day', periods=3).values
370382
result = algos.isin(arr, [arr[0]])
371383
expected = np.array([True, False, False])
372384
tm.assert_numpy_array_equal(result, expected)
373385

386+
result = algos.isin(arr, arr[0:2])
387+
expected = np.array([True, True, False])
388+
tm.assert_numpy_array_equal(result, expected)
389+
390+
result = algos.isin(arr, set(arr[0:2]))
391+
expected = np.array([True, True, False])
392+
tm.assert_numpy_array_equal(result, expected)
393+
374394
def test_large(self):
375395

376396
s = pd.date_range('20000101', periods=2000000, freq='s').values

0 commit comments

Comments
 (0)