Skip to content

Should Series.__contains__ look at indexes or values? #5571

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

Closed
danielballan opened this issue Nov 22, 2013 · 4 comments · Fixed by #6433
Closed

Should Series.__contains__ look at indexes or values? #5571

danielballan opened this issue Nov 22, 2013 · 4 comments · Fixed by #6433
Labels
Milestone

Comments

@danielballan
Copy link
Contributor

When writing this SO answer, I came upon this behavior, which I did not expect.

In [1]: 1 in Series([1])
Out[1]: False

In [2]: 1 in Series([1, 2])
Out[2]: True

Apparently, __contains__ looks at the index.

In [3]: 1 in Series([1], index=[1])
Out[3]: True

I can see how that makes sense. But I think it's fair to say that it is potentially confusing. Do we rely on this behavior of __contains__ for data alignment? Might it be reasonable to change it? I'm not sure that's a good idea, but I want to raise the issue.

@jreback
Copy link
Contributor

jreback commented Nov 22, 2013

IIRC this is for dict compat, IOW a series is dict like with the index being the keys and the values the actual values. So its consistent. For a frame, the __contains__ works on the columns axis.

isin is the way to example 'in' for values

@TomAugspurger
Copy link
Contributor

This may be a useful addition to http://pandas.pydata.org/pandas-docs/dev/gotchas.html

I don't know if anyone actually reads that, but at least it would be documented.

@jreback
Copy link
Contributor

jreback commented Nov 22, 2013

what, people don't read the docs???

:)

@jreback
Copy link
Contributor

jreback commented Feb 20, 2014

@danielballan want to do an addition to the gotchas (and/or section on Series is dict-like in basics.rst)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants