2017-06-06 25 views
3

这可能很基本,但为什么看起来像in不适用于包含对象或字符串的Series为什么在系列包含字符串时不能`in`搜索值

>>> import pandas as pd 

>>> s = pd.Series(['a', 'b', 'c']) 
>>> 'a' in s 
False 
>>> 'a' in s.astype('S1') 
False 

Series.__contains__文档是相当稀少:

[In 1]: s.__contains__? 
Signature: s.__contains__(key) 
Docstring: True if the key is in the info axis 
File:  c:\...\lib\site-packages\pandas\core\generic.py 
Type:  method 

我的第一个念头是in只检查了 “指标”:

>>> 1 in s 
True 

但后来:为什么它(似乎)使用其他类型:

>>> 1.2 in pd.Series([1.3, 1.2]) 
True 

>>> 1 in pd.Series([1.3, 1.2]) # also works for index 
True 

我对工作解决方案不感兴趣。我知道我可以简单地使用whatever in s.valuesnp.any(s.eq(whatever))。我想知道为什么它的行为方式(或者我错过了什么?)。

回答

3

它的行为方式是因为Series更像OrderedDict而不是列表。

就像1 in {0: 5, 1: 10}为真,1 in pd.Series([5, 10])也是如此,因为索引是RangeIndex(start=0, stop=2, step=1),索引元素就像键。

我看到为什么

>>> 1.2 in pd.Series([1.3, 1.2]) 
True 

的情况下可能会有点混乱,但它只是根据你所选择的数字巧合 - 1.2与任何比较之前强制转换为一个int RangeIndex或Int64Index,所以你真的要求1 in ser.index。就我个人而言,我不喜欢这种行为,但这就是它所做的。

>>> 1.9 in pd.Series([1.3, 1.2]) 
True 
>>> 1.2 in pd.Series([1.3, 1.2], index=[10, 20]) 
False 

为了使强制更加明显:

In [54]: np.inf in pd.Series([1.3, 1.2]) 
--------------------------------------------------------------------------- 
OverflowError        Traceback (most recent call last) 
<ipython-input-54-b069ecc5baf6> in <module>() 
----> 1 np.inf in pd.Series([1.3, 1.2]) 

[...] 
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.__contains__ (pandas/_libs/index.c:3924)() 

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.__contains__ (pandas/_libs/hashtable.c:13569)() 

OverflowError: cannot convert float infinity to integer 
相关问题