Skip to content

API: raise a TypeError when isin is passed a string #4765

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

Merged
merged 1 commit into from
Sep 6, 2013
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions doc/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ Indexing, iteration
DataFrame.pop
DataFrame.tail
DataFrame.xs
DataFrame.isin

Binary operator functions
~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
3 changes: 3 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ pandas 0.13
behavior.
- ``DataFrame.update()`` no longer raises a ``DataConflictError``, it now
will raise a ``ValueError`` instead (if necessary) (:issue:`4732`)
- ``Series.isin()`` and ``DataFrame.isin()`` now raise a ``TypeError`` when
passed a string (:issue:`4763`). Pass a ``list`` of one element (containing
the string) instead.

**Internal Refactoring**

Expand Down
5 changes: 5 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4609,6 +4609,11 @@ def isin(self, values, iloc=False):


else:
if not com.is_list_like(values):
raise TypeError("only list-like or dict-like objects are"
" allowed to be passed to DataFrame.isin(), "
"you passed a "
"{0!r}".format(type(values).__name__))
return DataFrame(lib.ismember(self.values.ravel(),
set(values)).reshape(self.shape),
self.index,
Expand Down
38 changes: 34 additions & 4 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2805,17 +2805,47 @@ def take(self, indices, axis=0, convert=True):

def isin(self, values):
"""
Return boolean vector showing whether each element in the Series is
exactly contained in the passed sequence of values
Return a boolean :ref:`~pandas.Series` showing whether each element in
the ref:`~pandas.Series` is exactly contained in the passed sequence of
``values``.

Parameters
----------
values : sequence
values : list-like
The sequence of values to test. Passing in a single string will
raise a ``TypeError``:

.. code-block:: python

from pandas import Series
s = Series(list('abc'))
s.isin('a')

Instead, turn a single string into a ``list`` of one element:

.. code-block:: python

from pandas import Series
s = Series(list('abc'))
s.isin(['a'])

Returns
-------
isin : Series (boolean dtype)
isin : Series (bool dtype)

Raises
------
TypeError
* If ``values`` is a string

See Also
--------
pandas.DataFrame.isin
"""
if not com.is_list_like(values):
raise TypeError("only list-like objects are allowed to be passed"
" to Series.isin(), you passed a "
"{0!r}".format(type(values).__name__))
value_set = set(values)
result = lib.ismember(_values_from_object(self), value_set)
return self._constructor(result, self.index, name=self.name)
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -10915,6 +10915,16 @@ def test_isin_dict(self):
expected.iloc[0, 0] = True
assert_frame_equal(result, expected)

def test_isin_with_string_scalar(self):
#GH4763
df = DataFrame({'vals': [1, 2, 3, 4], 'ids': ['a', 'b', 'f', 'n'],
'ids2': ['a', 'n', 'c', 'n']},
index=['foo', 'bar', 'baz', 'qux'])
with tm.assertRaises(TypeError):
df.isin('a')

with tm.assertRaises(TypeError):
df.isin('aaa')

if __name__ == '__main__':
# unittest.main()
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4433,6 +4433,16 @@ def test_isin(self):
expected = Series([True, False, True, False, False, False, True, True])
assert_series_equal(result, expected)

def test_isin_with_string_scalar(self):
#GH4763
s = Series(['A', 'B', 'C', 'a', 'B', 'B', 'A', 'C'])
with tm.assertRaises(TypeError):
s.isin('a')

with tm.assertRaises(TypeError):
s = Series(['aaa', 'b', 'c'])
s.isin('aaa')

def test_fillna_int(self):
s = Series(np.random.randint(-100, 100, 50))
s.fillna(method='ffill', inplace=True)
Expand Down