gpt4 book ai didi

在不一致的时间序列上使用 Pandas 的 Python EMA

转载 作者:太空宇宙 更新时间:2023-11-04 05:50:29 25 4
gpt4 key购买 nike

我正在尝试计算 Pandas 数据框中给定数据集的 EMA。我想要的 alpha 是 1 分钟,所以在理想情况下,我会将 60 的跨度传递给 EWMA 函数。

问题是,我的时间序列不一致 - 从某种意义上说,它不会“平滑地”从一秒移动到下一秒。例如-

(Date | Value)2015-05-27 05:14:35 | 52015-05-27 05:14:59 | 5.52015-05-27 05:15:30 | 5.22015-05-27 05:15:40 | 5.1

So a span of 60 obviously wouldn't apply here, as Pandas would just interpret that as every 60 datapoints rather than every 60 seconds. Are there any solutions beyond the obvious? The "obvious" being inserting datapoints for every second in the gaps, and extrapolating the values. I should note that the Date column is a proper Python datetime64 object.

My basic code...

import pandas

df = pandas.read_csv("data.csv")
dfe = pandas.ewma(df, span=60)

最佳答案

想通了。 @EdChum 推荐resample Pandas 中的方法,这就是我一直在寻找的。

import pandas

df = pandas.read_csv("data.csv")
dff = df.resample("S", fill_method='pad')

'fill_method' 选项防止"new"值成为 NaN。

所以现在像这样的数据框......

2015-05-27 05:14:35 | 52015-05-27 05:14:41 | 5.5

看起来像这样......

2015-05-27 05:14:35 | 52015-05-27 05:14:36 | 52015-05-27 05:14:37 | 52015-05-27 05:14:38 | 52015-05-27 05:14:39 | 52015-05-27 05:14:40 | 52015-05-27 05:14:41 | 5.5

关于在不一致的时间序列上使用 Pandas 的 Python EMA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30511147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com