Skip to content

Commit

Permalink
Merge pull request pandas-dev#6433 from danielballan/in-gotcha
Browse files Browse the repository at this point in the history
DOC: Add section on using in operator with Series, DataFrame
  • Loading branch information
jorisvandenbossche committed Mar 19, 2014
2 parents 8fc8b5a + b6858b9 commit 7e78d7c
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions doc/source/gotchas.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,30 @@ which is almost always what you want anyways.
See :ref:`boolean comparisons<basics.compare>` for more examples.

Using the ``in`` operator
~~~~~~~~~~~~~~~~~~~~~~~~~

Using the Python ``in`` operator on a Series tests for membership in the
index, not membership among the values.

.. ipython::

s = pd.Series(range(5), index=list('abcde'))
2 in s
'b' in s

If this behavior is surprising, keep in mind that using ``in`` on a Python
dictionary tests keys, not values, and Series are dict-like.
To test for membership in the values, use the method :func:`~pandas.Series.isin`:

.. ipython::

s.isin([2])
s.isin([2]).any()

For DataFrames, likewise, ``in`` applies to the column axis,
testing for membership in the list of column names.

``NaN``, Integer ``NA`` values and ``NA`` type promotions
---------------------------------------------------------

Expand Down

0 comments on commit 7e78d7c

Please sign in to comment.