如何重新采样时间序列?

时间:2017-03-02 10:19:05

标签: python pandas

使用pandas lib,如何将步长为1小时的时间序列转换为时间序列,步长为5分钟?

喜欢这个 Time step by hours

到此 enter image description here

要做到这一点,我尝试:

1)输入数据:

import pandas as pd

config = { "00:00": 9, "01:00": 2, "02:00": 0, "03:00": 2, "04:00": 126, "05:00": 1135, "06:00": 5591, "07:00": 10968, "08:00": 7711,
    "09:00": 3287, "10:00": 2652, "11:00": 2964, "12:00": 3959, "13:00": 3293, "14:00": 2625, "15:00": 3009, "16:00": 4563, "17:00": 5853,
    "18:00": 5065, "19:00": 2537, "20:00": 1214, "21:00": 483, "22:00": 211, "23:00": 67, "23:59": 9 }

2)构建列表:

list_of_keys = [key for (key,value) in config.items()]
list_of_values = [value for (key,value) in config.items()]

3)构建pandas.Serie

d_index = pd.DatetimeIndex(list_of_keys) 
# Here I don't know if I should use DatetimeIndx or PeriodIndex ...
# p_index = pd.PeriodIndex(d_index, freq='H')

d_serie = pd.Series(list_of_values, index=d_index, dtype='int64')

在此之后,我不知道如何前进...... 我尝试使用系列的重新采样功能,但没有结果。

1 个答案:

答案 0 :(得分:0)

你需要:

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

#create series by dict
d_serie = pd.Series(config)
#convert index to TimedeltaIndex
d_serie.index = pd.to_timedelta(d_serie.index.astype(str) + ':00')
#print (d_serie)

#upsampling with forward filling NaNs
s = d_serie.resample('5Min').ffill() 
#plot bar
ax = s.plot.bar()

#change format of values of axis x
ticklabels = pd.to_datetime(s.index.values).strftime('%H:%M')
ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))

#solution for many values in axis
#show only each 30th label, another are not visible
spacing = 30
visible = ax.xaxis.get_ticklabels()[::spacing]
for label in ax.xaxis.get_ticklabels():
    if label not in visible:
        label.set_visible(False)

plt.show()

graph