两列时间序列数据的标准差
Standard deviation of time series data on two columns
我有一个数据框,其中包含一天的两列数据和时间序列索引。样本数据在 1 分钟内,我想创建一个 5 分钟的数据框,当相应 5 分钟内 5 个样本的标准偏差未偏离 5% 时,5 分钟间隔将被标记为假5 个样本的平均值,这需要在一天中的每个 5 分钟和每一列中执行。如下所示,对于 DF1 列 X,我们计算了从 16:01 到 16:05 的 5 个样本的均值和标准差,我们看到 %(Std/Mean) 并且将对接下来的 5 个样本和 y 列。如果 %(std/Mean)>5% 那么 DF2 将被填充,那么特定的 5 分钟间隔将为假。
您可以使用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)
我有一个数据框,其中包含一天的两列数据和时间序列索引。样本数据在 1 分钟内,我想创建一个 5 分钟的数据框,当相应 5 分钟内 5 个样本的标准偏差未偏离 5% 时,5 分钟间隔将被标记为假5 个样本的平均值,这需要在一天中的每个 5 分钟和每一列中执行。如下所示,对于 DF1 列 X,我们计算了从 16:01 到 16:05 的 5 个样本的均值和标准差,我们看到 %(Std/Mean) 并且将对接下来的 5 个样本和 y 列。如果 %(std/Mean)>5% 那么 DF2 将被填充,那么特定的 5 分钟间隔将为假。
您可以使用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)