pandas将系列与numpy float进行比较会产生TypeError

时间:2016-07-23 03:46:58

标签: python numpy pandas

pandas中,您可以这样做:

>>> x = pd.DataFrame([[1,2,3,4], [3,4,5,6]], columns=list('abcd'))
>>> x
   a  b  c  d
0  1  2  3  4
1  3  4  5  6
>>> 2 < x.a
0    False
1     True
Name: a, dtype: bool

然而,当我使用numpy float:

尝试它时
>>> np.float64(2) < x.a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/simon/Documents/workspace/rent-my-rez/venv/lib/python2.7/site-packages/pandas/core/ops.py", line 741, in wrapper
    if len(self) != len(other):
TypeError: len() of unsized object

是否有某种解决方法(它不会将numpy浮点数转换为常规浮点数),或者我可以通过某种方式修补pandas中的Series类以实现反向比较呢?我已经在源代码中查看了实现比较的位置,但是我无法找到它,所以在代码中对位置的引用会非常有帮助

(我知道通过改变比较的顺序很容易解决,但我更感兴趣的是,因为我想更多地了解源代码)

2 个答案:

答案 0 :(得分:1)

也许您应该将您的pandas版本更新到最新版本,因为您的错误有更新。我只是在Python 2.7.3中用pip更新它0.19.2。现在修复了错误。

答案 1 :(得分:0)

这似乎是一个已知的问题here并修复here,如果你(像我一样)正在运行0.18.0并试图找到github上的等效行。如果您在0.18.0而不是master查看github,则可以看到相关的行,例如第739行here