【发布时间】:2018-08-02 15:06:41
【问题描述】:
我正在处理数据框中的一些数据,我发现自己编写的代码包含很多:
def entry_signal(y):
conditions1 = [np.logical_and(y > 0, y.shift(1) < 0),np.logical_and(y < 0, y.shift(1) > 0)]
values1 = ['LongEntry','ShortEntry']
return np.select(conditions1, values1, '')
基本上,如果该值超过 0 并且前一个值小于 0,则它应该为 true。
我尝试创建一个执行此操作的函数,但我不断收到错误:
def cross_above(x,y):
if np.logical_and(x>y, x.shift(1)<y):
return True
else:
return False
然后我尝试在这里使用它:
def entry_signal(y):
conditions1 = [cross_above((y,0), y.shift(1) < 0),np.logical_and(y < 0, y.shift(1) > 0)]
values1 = ['LongEntry','ShortEntry']
return np.select(conditions1, values1, '')
但我不断得到一个系列的价值真相是模棱两可的。我做错了什么?
【问题讨论】:
-
然后我得到:TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]
-
发布您的数据
.head().to_dict()以便我们重现?
标签: python python-3.x pandas function numpy