Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Resampling Hourly Data into Half Hourly in Pandas

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading