Skip to main content
 首页 » 编程设计

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

2025年05月04日8mfryf

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

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

(Date | Value) 
2015-05-27 05:14:35 | 5 
2015-05-27 05:14:59 | 5.5 
2015-05-27 05:15:30 | 5.2 
2015-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 | 5 
2015-05-27 05:14:41 | 5.5 

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

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