Why does `False in pandas.Series([True,True])` return True?

前端 未结 3 1392
臣服心动
臣服心动 2021-01-22 13:13
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

相关标签:
3条回答
  • 2021-01-22 13:33

    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]: False in pd.Series([True,True])
    Out[286]: True
    

    is the same as:

    In [287]: 0 in pd.Series([True,True])
    Out[287]: True
    

    and

    In [288]: True in pd.Series([True,True])
    Out[288]: True
    

    is the same as:

    In [290]: 1 in pd.Series([True,True])
    Out[290]: True
    

    but

    In [291]: 2 in pd.Series([True,True])
    Out[291]: False
    

    because there is no such index in this series:

    In [292]: pd.Series([True,True])
    Out[292]:
    0    True
    1    True
    dtype: bool
    

    UPDATE:

    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]: False
    
    In [297]: (pd.Series([True, True]) == True).any()
    Out[297]: True
    
    0 讨论(0)
  • 2021-01-22 13:36
    >>> help(pd.Series([True, True]).__contains__)
    Help on method __contains__ in module pandas.core.generic:
    
    __contains__(key) method of pandas.core.series.Series instance
        True if the key is in the info axis
    
    >>> pd.Series([True, True])
    0    True
    1    True
    dtype: bool
    
    ^
    info axis
    
    0 讨论(0)
  • 2021-01-22 13:47

    If you are checking for more than one value, I highly recommend the isin method.

    >>> pd.Series(range(10)).isin([1]).any()
    True
    
    0 讨论(0)
提交回复
热议问题