在 pandas 中将每小时数据上采样为 5 分钟数据

Upsampling hourly data to 5 minute data in pandas

我有以下数据:

                              MTU (CET)  Day-ahead Price [EUR/MWh]
0   09.10.2017 00:00 - 09.10.2017 01:00                      43.13
1   09.10.2017 01:00 - 09.10.2017 02:00                      34.80
2   09.10.2017 02:00 - 09.10.2017 03:00                      33.31
3   09.10.2017 03:00 - 09.10.2017 04:00                      32.24
              .......
22  09.10.2017 22:00 - 09.10.2017 23:00                      49.06
23  09.10.2017 23:00 - 10.10.2017 00:00                      38.46

我希望每 5 分钟从中获取一次数据。 通过使用:

    price = pd.read_csv(price_data)
    price_x = price.set_index(pd.DatetimeIndex(price['MTU (CET)'].str[:-19]))
    price2 = price_x.resample('300S').pad()

我得到以下数据:

2017-09-10 00:00:00    43.13
2017-09-10 00:05:00    43.13
2017-09-10 00:10:00    43.13
                   ...  

2017-09-10 22:45:00    49.06
2017-09-10 22:50:00    49.06
2017-09-10 22:55:00    49.06
2017-09-10 23:00:00    38.46

但是,对于 23:00 和 00:00 之间的分钟,价格也应该是 38.46。有人知道怎么帮忙吗?

您需要手动添加下一个小时的最后一行以及 iloc 选择的最后一行的数据:

price_x = price.set_index(pd.DatetimeIndex(price['MTU (CET)'].str[:-19]))
price_x.loc[price_x.index[-1] + pd.Timedelta(1, unit='h')] = price_x.iloc[-1]

print (price_x.tail(3))

                     Day-ahead Price [EUR/MWh]  
MTU (CET)                                       
2017-09-10 22:00:00                      49.06  
2017-09-10 23:00:00                      38.46  
2017-09-11 00:00:00                      38.46  

price2 = price_x.resample('300S').pad()
print (price2.tail(20))

                     Day-ahead Price [EUR/MWh]  
MTU (CET)                                       
2017-09-10 22:25:00                      49.06  
2017-09-10 22:30:00                      49.06  
2017-09-10 22:35:00                      49.06  
2017-09-10 22:40:00                      49.06  
2017-09-10 22:45:00                      49.06  
2017-09-10 22:50:00                      49.06  
2017-09-10 22:55:00                      49.06  
2017-09-10 23:00:00                      38.46  
2017-09-10 23:05:00                      38.46  
2017-09-10 23:10:00                      38.46  
2017-09-10 23:15:00                      38.46  
2017-09-10 23:20:00                      38.46  
2017-09-10 23:25:00                      38.46  
2017-09-10 23:30:00                      38.46  
2017-09-10 23:35:00                      38.46  
2017-09-10 23:40:00                      38.46  
2017-09-10 23:45:00                      38.46  
2017-09-10 23:50:00                      38.46  
2017-09-10 23:55:00                      38.46  
2017-09-11 00:00:00                      38.46