Skip to content

Commit 7e78d7c

Browse files
Merge pull request pandas-dev#6433 from danielballan/in-gotcha
DOC: Add section on using in operator with Series, DataFrame
2 parents 8fc8b5a + b6858b9 commit 7e78d7c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

doc/source/gotchas.rst

+24
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,30 @@ which is almost always what you want anyways.
8888
8989
See :ref:`boolean comparisons<basics.compare>` for more examples.
9090

91+
Using the ``in`` operator
92+
~~~~~~~~~~~~~~~~~~~~~~~~~
93+
94+
Using the Python ``in`` operator on a Series tests for membership in the
95+
index, not membership among the values.
96+
97+
.. ipython::
98+
99+
s = pd.Series(range(5), index=list('abcde'))
100+
2 in s
101+
'b' in s
102+
103+
If this behavior is surprising, keep in mind that using ``in`` on a Python
104+
dictionary tests keys, not values, and Series are dict-like.
105+
To test for membership in the values, use the method :func:`~pandas.Series.isin`:
106+
107+
.. ipython::
108+
109+
s.isin([2])
110+
s.isin([2]).any()
111+
112+
For DataFrames, likewise, ``in`` applies to the column axis,
113+
testing for membership in the list of column names.
114+
91115
``NaN``, Integer ``NA`` values and ``NA`` type promotions
92116
---------------------------------------------------------
93117

0 commit comments

Comments
 (0)