Python 如何对时间序列重新采样?

Python 如何对时间序列重新采样?,python,pandas,Python,Pandas,使用pandas lib,如何将步长为1小时的时间序列转换为步长为5分钟的时间序列 像这样 到此 为此,我尝试: 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":

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

像这样

到此

为此,我尝试:

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) 建造熊猫系列

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')
之后,我不知道如何前进。。。 我尝试使用序列的重采样功能,但没有结果。

您需要:

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()

与FYI一样,刺激12倍的过采样不会产生新信息。除非手动添加噪波,否则将永远无法从第一个图形中获得第二个图形,如图所示。充其量,你可以做一些插值。您希望实现什么?另外,
list(config.keys())
list(config.values())
这是XY问题的一个很好的候选。你能提供一些背景资料吗?