Skip to content
Advertisement

How to group by time-interval from bottom to top using Pandas resample functionality?

I am working with historic data of some stocks. I want to group data by certain time intervals (like 1hr, 3days, etc). Pandas gives amazing functionality of doing this with very less efforts using resampling. But it happens from top-to-bottom (below image). Like –

JavaScript

enter image description here

Here, I want to group from bottom-to-top, like –

JavaScript

How can I do this with pandas resampling? If there is another way of doing this please mention it as well. Thanks :)

EDIT: I want something like this out of above image data –

JavaScript

Advertisement

Answer

Maybe you can use the iloc to reverse after resample? I’m not sure if that hinders your further calculations, but it can resample and reverse the set.

Since I do not have access to your exact sample data

Here’s how I am testing it:

JavaScript

This results in the 1 min df:

1min df

You can then apply resample and the iloc:

JavaScript

This results in a reversed df:

reversed and resampled

Advertisement