两列时间序列数据的标准差

问题描述 投票:0回答:1

我有一个数据框架,有一天的两列数据,有一个时间序列指数。样本数据是以1分钟为单位的,我想创建一个5分钟的数据框架,当5个样本在5分钟内的标准差没有偏离5个样本平均值的5%时,5分钟的区间将被标记为假,这需要对一天中的每个5分钟和每个列执行。如下图所示,对于DF1列X,我们计算从16:01到16:05的5个样本的平均数和标准差,我们看到%(StdMean),同样的事情将被做给接下来的5个样本和列y。然后DF2将被填充,如果%(stdMean)>5%,那么特定的5分钟区间将是假的。

enter image description here

python pandas statistics time-series standard-deviation
1个回答
1
投票

你可以使用pandas数据帧的重采样方法,数据帧最是有时间戳的索引。这里有一个例子。

import pandas as pd
import numpy as np
dates = pd.date_range('1/1/2020', periods=30)
df = pd.DataFrame(np.random.randn(30,2), index=dates, columns=['X','Y'])
df.head()

lbl = 'right' # set the label of the window index to the value of the right
w = '3d'
threshold = 1 # here goes your threshold for flagging the ration of standard deviation and mean
x=df.resample(w, label=lbl).std()['X'] / df.resample(w, label=lbl).mean()['X'] > threshold
y=df.resample(w, label=lbl).std()['Y'] / df.resample(w, label=lbl).mean()['Y'] > threshold

DF2 = pd.concat([x,y], axis=1) 
© www.soinside.com 2019 - 2024. All rights reserved.