二者都
self.close
和
self.ema
是
DataFrame
系列比较两个系列(例如。
self.close > self.ema
)结果为布尔级数:
a = pd.Series([1, 2, 3, 4])
b = pd.Series([3, 1, 4, 2])
print(a > b) # Outputs pd.Series([False, True, False, True])
在if语句中使用布尔序列(例如。
if a > b:
)是模棱两可的,因为Pandas不知道你的意思:如果所有项目都是true,那么这个表达式应该评估为true吗?如果至少有一项为true,它的值应该为true?
因此,您需要将其与
.any()
或
.all()
:
if (a > b).all(): # If all items in `a` are greater than
pass # their corresponding items in `b`
if (a > b).any(): # If any item in `a` is greater than
pass # their corresponding item in `b`
这会让阅读您代码的人和您自己更清楚地了解您的意图。