Upsampling hourly data to 5 minute data in pandas

半世苍凉 提交于 2021-01-01 04:18:38

问题


I have the following data:

                              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

From which I would like to have data for every 5 minutes. By using:

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

I get the following data:

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

However, for the minutes between 23:00 and 00:00 the price should also be 38.46. Does anyone know how to help?


回答1:


You need manually add last row with next hour and with data from last row seelcted by 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  


来源:https://stackoverflow.com/questions/46666464/upsampling-hourly-data-to-5-minute-data-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!