• [x] I have checked that this issue has not already been reported.

  • [x] I have confirmed this bug exists on the latest version of pandas.

  • [x] (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas
series = pandas.Series([1])
series.index = index=pandas.date_range("20180101", periods=1)
inOperatorOutput = 1 in series
listOperatorOutput = 1 in list(series)
assert inOperatorOutput == listOperatorOutput

Problem description

This code yields an assertion error. Because the in operator looks into the index of a series while the list function will take the values. This is not the same way how python lists and other python objects work and therefore it confuses people. I think the best solution would be to let the in operator take the values instead of the index.

FYI, here is an SO thread about this topic: https://stackoverflow.com/questions/51962778/python-in-operator-not-working-as-expected-when-comparing-string-and-strftime-va/51962950?noredirect=1#comment121535570_51962950

Comment From: mroeschke

Thanks for the report. This behavior has been the behavior and documented for quite some time, so changing this behavior would be a huge breaking change to the library. https://pandas.pydata.org/pandas-docs/stable/user_guide/dsintro.html#series-is-dict-like

Closing as this has been the documented behavior for a while.

Comment From: tbrodbeck

I think you understood me wrong: Try this code, for a dictionary the operators are consistent:

dic = {'someIndex': 1}
inOperatorOutput = 1 in dic
listOperatorOutput = 1 in list(dic)
assert inOperatorOutput == listOperatorOutput

The problem is a series is also like a list in some cases. For example when it is type casted into a list.

Better change it now then later ;) Maybe you could put this to the next major update?

Comment From: mroeschke

A Series doesn't subclass dict, so it doesn't necessarily implement all the behaviors of a dict ("dict-like")

I'll re-open this issue up as an API change which would require deprecating this existing behavior. It would need buy in from the other core team member before moving forward with the deprecation. I would be -0.5 personally since I am not sure if changing this behavior is worth the churn.

Comment From: jreback

single element membership is an antipattern -1 on changing this which may we'll be impossible - what would u deprecate

Comment From: jbrockmendel

Deprecating and telling users to check item in series.index wouldn't be the worst thing, but the future behavior would definitely be to raise, not to change to behave like item in series._values