【发布时间】:2018-08-02 08:28:18
【问题描述】:
我有我的时间序列数据,我想在 t+5 和 t-5 的窗口中检查数据并检查它是否在 0.1 和 5 之间,然后需要将该时间标记为 1,同样如果同一窗口中的值大于 5 则应返回 2 否则返回 0。
我试过这样,请问是否有更有效的方法可以做到这一点。
def my_func(arr,thres=5,lwthres=0.1):
arr=arr.astype(float)
if((arr[0]<thres) & (arr[1]<thres) & (arr[2]<thres) &(arr[3]<thres) &(arr[4]<thres)\
&(arr[5]<thres)&(arr[6]<thres)&(arr[7]<thres)&(arr[8]<thres)&(arr[9]<thres)\
& (arr[0]>=lwthres) & (arr[1]>=lwthres) & (arr[2]>=lwthres) &(arr[3]>=lwthres)\
& (arr[4]>lwthres) &(arr[5]>=lwthres)&(arr[6]>=lwthres)&(arr[7]>=lwthres)&(arr[8]>=lwthres)&(arr[9]>=lwthres)):
return 1
elif((arr[0]>=thres) & (arr[1]>=thres) & (arr[2]>=thres) &(arr[3]>=thres) &(arr[4]>=thres) &(arr[5]>=thres)&(arr[6]>=thres)&(arr[7]>=thres)&(arr[8]>=thres)&(arr[9]>=thres)):
return 2
else:
return 0
my_data=np.random.randint(5,size=100000)
my_df=pd.DataFrame(my_data)
tp=my_df.rolling(window=10,center=True).apply(lambda x:my_func(x))
df=pd.DataFrame()
df['value']=my_data
df['Type']=tp
【问题讨论】:
标签: python pandas numpy time-series