Why Does `false In Pandas.series([true,true])` Return True?
False in [True,True] False in pd.Series([True,True]) the first line of code returns False the second line of code returns True! I think I must be doing something wrong or missing
Solution 1:
You are checking whether 0 (internal False representation) is in the Series's index - @Uriel has shown a docstring explaining why is it happening:
In [286]: Falsein pd.Series([True,True])
Out[286]: Trueis the same as:
In [287]: 0in pd.Series([True,True])
Out[287]: Trueand
In [288]: Truein pd.Series([True,True])
Out[288]: Trueis the same as:
In [290]: 1in pd.Series([True,True])
Out[290]: Truebut
In [291]: 2in pd.Series([True,True])
Out[291]: Falsebecause there is no such index in this series:
In [292]: pd.Series([True,True])
Out[292]:
0True1True
dtype: boolUPDATE:
if you want to check whether at least one series element is False or True:
In [296]: (pd.Series([True, True]) ==False).any()
Out[296]: FalseIn [297]: (pd.Series([True, True]) ==True).any()
Out[297]: TrueSolution 2:
>>> help(pd.Series([True, True]).__contains__)
Help on method __contains__ inmodule pandas.core.generic:
__contains__(key) method of pandas.core.series.Series instance
Trueif the keyisin the info axis
>>> pd.Series([True, True])
0True1Truedtype: bool
^
info axis
Solution 3:
If you are checking for more than one value, I highly recommend the isin method.
>>> pd.Series(range(10)).isin([1]).any()
True
Post a Comment for "Why Does `false In Pandas.series([true,true])` Return True?"