[英]python dataframe timeseries check if value changed more than x in last n rows and forward n rows
I have a sunlight data coming from the field.我有来自现场的阳光数据。 I am checking if the sunlight changed more than a value in last 1 min and future 1 min.我正在检查阳光在过去 1 分钟和未来 1 分钟内的变化是否超过一个值。 Below I am giving an example case.下面我举一个例子。 Where I am checking if the data value changed more than 4 in the last 10s.我在哪里检查数据值在过去 10 秒内是否变化超过 4。 code:代码:
xdf = pd.DataFrame({'data':np.random.randint(10,size=10)},index=pd.date_range('2022-06-03 00:00:00', '2022-06-03 00:00:45', freq='5s'))
# here data frequency 5s, so, to check last 10s
# I have to consider present row and last 2 rows
# Perform rolling max and min value for 3 rows
nrows = 3
# Allowable change
ac = 4
xdf['back_max'] = xdf['data'].rolling(nrows).max()
xdf['back_min'] = xdf['data'].rolling(nrows).min()
xdf['back_min_max_dif'] = (xdf['back_max'] - xdf['back_min'])
xdf['back_<4'] = (xdf['back_max'] - xdf['back_min']).abs().le(ac)
print(xdf)
## Again repeat the above for the future nrows
## Don't know how?
expected output:预期输出:
data back_max back_min back_min_max_dif back_<4
2022-06-03 00:00:00 7 NaN NaN NaN False
2022-06-03 00:00:05 7 NaN NaN NaN False
2022-06-03 00:00:10 5 7.0 5.0 2.0 True
2022-06-03 00:00:15 8 8.0 5.0 3.0 True
2022-06-03 00:00:20 6 8.0 5.0 3.0 True
2022-06-03 00:00:25 2 8.0 2.0 6.0 False
2022-06-03 00:00:30 3 6.0 2.0 4.0 True
2022-06-03 00:00:35 1 3.0 1.0 2.0 True
2022-06-03 00:00:40 5 5.0 1.0 4.0 True
2022-06-03 00:00:45 5 5.0 1.0 4.0 True
Is there way I can simplify the above procedure?有没有办法可以简化上述程序? Also, I have to perform rolling max for future nrows, and how?另外,我必须为未来的 nrows 执行最大滚动,如何?
For future/forward roll, you can roll on the reversed data.对于未来/向前滚动,您可以滚动反向数据。 This might not work with time-window roll:这可能不适用于时间窗口滚动:
rolling = xdf['data'].rolling(nrows)
xdf['pass_<'] = (rolling.max()-rolling.min()).le(ac)
future_roll = xdf['data'][::-1].rolling(nrows)
xdf['future_<'] = future_roll.max().sub(future_roll.min()).le(ac)
Output:输出:
data pass_< future_<
2022-06-03 00:00:00 7 False True
2022-06-03 00:00:05 7 False True
2022-06-03 00:00:10 5 True True
2022-06-03 00:00:15 8 True False
2022-06-03 00:00:20 6 True True
2022-06-03 00:00:25 2 False True
2022-06-03 00:00:30 3 True True
2022-06-03 00:00:35 1 True True
2022-06-03 00:00:40 5 True False
2022-06-03 00:00:45 5 True False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.