2014-06-29 144 views
1

我有以下数据帧df熊猫:德seasonalizing时间序列数据

[OUT]:

     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在每10秒的非连续22天由体积观测。我想通过将每个观测值除以他们各自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之间的平均值是9:30:00至9:35之间的总体积:00(第1天+第2天+第3天......第22天)/ 22。是否合理?)。然后我将这个时间间隔的平均值除以9:30:00 - 9:35:00之间的df中的每个观察值。

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

回答

4

编辑答案:

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}) 
+0

没有这将是刚在每5分钟连续的平均值整个样本。我需要整个时间序列中每5分钟的平均值。所以9:30:00到9:35:00之间的平均值是9:30:00到9:35:00(第1天+第2天+第3天......第22天)/ 22之间的总体积。这有意义吗?感谢您的尝试 – Plug4

+0

更新后的答案是否解决了这个问题? – Zero

+0

看起来不错!谢谢! – Plug4