在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
类以实现反向比较呢?我已经在源代码中查看了实现比较的位置,但是我无法找到它,所以在代码中对位置的引用会非常有帮助
(我知道通过改变比较的顺序很容易解决,但我更感兴趣的是,因为我想更多地了解源代码)