2

我有以下数据结构:

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 库可以帮助实现这种数据转换?

4

1 回答 1

3
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
于 2012-05-10T12:16:19.337 回答