Home > database >  Resampling Hourly Data into Half Hourly in Pandas
Resampling Hourly Data into Half Hourly in Pandas

Time:01-28

I have the following DataFrame called prices:

DateTime              PriceAmountGBP
0   2022-03-27 23:00:00 202.807890
1   2022-03-28 00:00:00 197.724150
2   2022-03-28 01:00:00 191.615328
3   2022-03-28 02:00:00 188.798436
4   2022-03-28 03:00:00 187.706682
... ... ...
19  2023-01-24 18:00:00 216.915400
20  2023-01-24 19:00:00 197.050516
21  2023-01-24 20:00:00 168.227992
22  2023-01-24 21:00:00 158.954200
23  2023-01-24 22:00:00 149.039322

I'm trying to resample prices to show Half Hourly data instead of Hourly, with PriceAmountGBP repeating on the half hour, desired output below:

DateTime                PriceAmountGBP
0   2022-03-27 23:00:00 202.807890
1   2022-03-28 23:30:00 202.807890
2   2022-03-28 00:00:00 197.724150
3   2022-03-28 00:30:00 197.724150
4   2022-03-28 01:00:00 191.615328
... ... ...
19  2023-01-24 18:00:00 216.915400
20  2023-01-24 18:30:00 216.915400
21  2023-01-24 19:00:00 197.050516
22  2023-01-24 19:30:00 197.050516
23  2023-01-24 20:00:00 168.227992

I've attempted the below which is incorrect:

prices.set_index('DateTime').resample('30T').interpolate()

Output:

                PriceAmountGBP
DateTime    
2022-03-27 23:00:00 202.807890
2022-03-27 23:30:00 200.266020
2022-03-28 00:00:00 197.724150
2022-03-28 00:30:00 194.669739
2022-03-28 01:00:00 191.615328
... ...
2023-01-24 20:00:00 168.227992
2023-01-24 20:30:00 163.591096
2023-01-24 21:00:00 158.954200
2023-01-24 21:30:00 153.996761
2023-01-24 22:00:00 149.039322

Any help appreciated!

CodePudding user response:

You want to resample without any transformation, and then do a so-called "forward fill" of the resulting null values.

That's:

result = (
    prices.set_index('DateTime')
       .resample('30T')
       .asfreq()  # no transformation
       .ffill()   # drag previous values down
)
  • Related