我不明白为什么Pandas在比较数据框时不超载/不允许使用内置于python的“ and”和“ or”运算符
import pandas as pd
In [4]: d = {'a':[True,False], 'b':[False,False]}
In [5]: x = pd.DataFrame(d)
In [6]: x['a'] & x['b']
Out[6]:
0 False
1 False
dtype: bool
In [7]: x['a'] and x['b']
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-0ee6588ad92b> in <module>
----> 1 x['a'] and x['b']
/usr/local/lib/python3.7/site-packages/pandas/core/generic.py in __nonzero__(self)
1477 def __nonzero__(self):
1478 raise ValueError(
-> 1479 f"The truth value of a {type(self).__name__} is ambiguous. "
1480 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
1481 )
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
In [8]:
不可以 你不能超载。重载与短路语义的交互作用很差,因此Python不允许这样做。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句