问题描述
我正在处理具有以下列的熊猫数据框-
- 日期
- A
- 在A上滚动窗口
我希望A上的滚动窗口在达到某个值后重置,然后从该点开始计算滚动窗口。到目前为止,这是我在代码方面提出的,但是,返回条件从未达到-从而不会返回包含达到阈值的行的列表。
此外,我确实在此问题的答案-Creating a rolling sum column that resets once it reaches a threshold中签出了该实现,但它不起作用。
def window_calc(window_size,df):
#Input - rolling window size,dataframe
#Output - dataframe with computed rolling window
df['Rolling Window'] = df['A'].rolling(window=window_size).sum()
return df
def reset_window(window_size,df,end_date,end_index,large_move=[]):
#Input - rolling window size,input dataframe
#Output - list containing large moves
temp = window_calc(window_size,df)
mask_1 = np.abs(temp['Rolling Window'])>=0.2 #this is the value at which I want the rolling window to reset
t = temp[mask_1]
check = False
#print(t)
if not t.empty and not temp.empty:
large_move.append(t.iloc[0])
elif t.empty or temp.empty:
check = True
#print(large_move)
if not check and temp.tail(1).index.item() - end_index < window_size:
if t.iloc[0]['date']<end_date-timedelta(days=window_size):
mask_2 = temp['date']>t.iloc[0]['date']
temp_2 = temp[mask_2]
reset_window(window_size,temp_2,end_index)
else:
return large_move
当我进行打印调试时,large_move列表确实包含大动作,但该列表从不返回。为了进行调试,我在最终的else条件中放置了一条打印语句,它确实在语句中打印了值。
有人可以帮我这个忙吗?谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)