【问题标题】:Standard deviation of time series data on two columns两列时间序列数据的标准差
【发布时间】:2020-08-11 11:35:00
【问题描述】:

我有一个数据框,其中包含一天的两列数据,并带有时间序列索引。样本数据在 1 分钟内,我想创建一个 5 分钟的数据框,当相应 5 分钟内 5 个样本的标准偏差不偏离 5% 时,5 分钟间隔将被标记为假5 个样本的平均值,这需要在一天中的每个 5 分钟和每一列中执行。如下所示,对于 DF1 列 X,我们计算了 16:01 到 16:05 的 5 个样本的平均值和标准偏差,我们看到了 %(Std/Mean),接下来的 5 个样本和列将执行相同的操作是的。如果 %(std/Mean)>5% 则将填充 DF2,则特定的 5 分钟间隔将为 false。

【问题讨论】:

  • Krish,这将有助于查看一些示例数据,以及一些您想要实现的数字示例
  • 嗨,我已经添加了示例数据以及我想要实现的目标。

标签: python pandas statistics time-series standard-deviation


【解决方案1】:

您可以使用 pandas 数据帧的 resample 方法,因为数据帧大多数是带有时间戳的索引。举个例子:

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) 

【讨论】:

  • 感谢您的回复。如果我有任何问题会回复。
  • 嗨,Krish,您只需插入您的数据,它就可以工作了。我每天都使用数据点使示例变得更简单。但是,可以简单地将dates 的生成更改为pd.date_range('17/1/2020 16:01', freq='min', periods=10 - 请记住:stackoverflow.com/help/someone-answers
猜你喜欢
  • 2013-02-03
  • 1970-01-01
  • 1970-01-01
  • 2015-05-12
  • 2013-10-15
  • 1970-01-01
  • 2021-10-01
  • 1970-01-01
  • 2017-12-14
相关资源
最近更新 更多