我有以下数据结构:
2011-01-01 00:00, 2011-01-20 00:00, 200 # days-range
2011-01-20 00:00, 2011-03-08 00:00, 1288 # days-range
2011-04-11 00:00, 2012-01-08 00:00, 5987 # days-range
2012-02-01 00:00, 2012-02-01 01:00, 7 # hourly-range
2012-02-01 02:00, 2012-02-01 02:30, 3 # hourly-range
这是开始日期,结束日期和值的时间间隔(日期之间记录的某些指标)。
对于进一步的数据分析,我需要生成所需频率的时间序列: 每月/每日/每小时/半小时时间序列。例如,每小时数据:
2011-01-01 00:00, 2
2011-01-01 01:00, 6
2011-01-01 02:00, 5
...
是否有任何python库可以帮助实现这种数据转换?
答案 0 :(得分:2)
import pandas as pd
def stretch(start_date, end_date, value, freq):
freq_dict = {'d': pd.datetools.day,
'h': pd.datetools.Hour(1)}
dr = pd.DateRange(start_date, end_date, offset=freq_dict[freq])
return pd.TimeSeries(value / dr.size, index=dr)
print stretch('2011-01-01 00:00', '2011-01-20 00:00', 200, 'd')
打印
2011-01-01 10
2011-01-02 10
2011-01-03 10
2011-01-04 10
2011-01-05 10
2011-01-06 10
2011-01-07 10
2011-01-08 10
2011-01-09 10
2011-01-10 10
2011-01-11 10
2011-01-12 10
2011-01-13 10
2011-01-14 10
2011-01-15 10
2011-01-16 10
2011-01-17 10
2011-01-18 10
2011-01-19 10
2011-01-20 10