gpt4 book ai didi

python - Pandas:去季节性时间序列数据

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

我有以下数据框df:

[输出]:

                     VOL
2011-04-01 09:30:00 11297
2011-04-01 09:30:10 6526
2011-04-01 09:30:20 14021
2011-04-01 09:30:30 19472
2011-04-01 09:30:40 7602
...
2011-04-29 15:59:30 79855
2011-04-29 15:59:40 83050
2011-04-29 15:59:50 602014

df 包含连续 22 天每 10 秒一次的体积观测值。我想通过将每个观察值除以各自 5 分钟时间间隔的平均量来对我的时间序列进行去季节性化处理。为此,我需要在 22 天内每 5 分钟取一次时间序列平均成交量。所以我最终会在每 5 分钟 9:30:00 - 9:35:00 得到一个时间序列的平均值; 9:35:00 - 9:40:00; 9:40:00 - 9:45:00 ... 直到 16:00:00。 9:30:00 - 9:35:00 时间间隔的平均值是所有 22 天该时间间隔的平均音量(即 9:30:00 到 9 之间的平均值: 35:00 是 (day 1 + day 2 + day 3 ... day 22)/22 的 9:30:00 到 9:35:00 之间的总成交量。这有意义吗?)。然后,我会将 df9:30:00 - 9:35:00 之间的每个观察值除以该时间间隔的平均值。

Python/Pandas 中是否有可以执行此操作的包?

最佳答案

编辑后的答案:

date_times = pd.date_range(datetime.datetime(2011, 4, 1, 9, 30),
datetime.datetime(2011, 4, 16, 0, 0),
freq='10s')
VOL = np.random.sample(date_times.size) * 10000.0

df = pd.DataFrame(data={'VOL': VOL,'time':date_times}, index=date_times)
df['h'] = df.index.hour
df['m'] = df.index.minute
df1 = df.resample('5Min', how={'VOL': np.mean})
times = pd.to_datetime(df1.index)
df2 = df1.groupby([times.hour,times.minute]).VOL.mean().reset_index()
df2.columns = ['h','m','VOL']
df.merge(df2,on=['h','m'])
df_norm = df.merge(df2,on=['h','m'])
df_norm['norm'] = df_norm['VOL_x']/df_norm['VOL_y']

** 较旧的答案(暂时保留)

使用重采样函数

df.resample('5Min', how={'VOL': np.mean})

例如:

date_times = pd.date_range(datetime.datetime(2011, 4, 1, 9, 30),
datetime.datetime(2011, 4, 16, 0, 0),
freq='10s')
VOL = np.random.sample(date_times.size) * 10000.0

df = pd.DataFrame(data={'VOL': VOL}, index=date_times)
df.resample('5Min', how={'VOL': np.mean})

关于python - Pandas:去季节性时间序列数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24476600/

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