So I have a stock OHLC & Volume data on 15 minute timeframe. I would like to groupby it by time and then calculate the rank of volume of last 10 periods. So 930 am will check the last 10 days 930 row and rank it accordingly

Date_time   Open    High    Low Close   Volume
0   2015-02-02 09:29:00 585.95  592.85  584.60  592.40  218485
1   2015-02-02 09:44:00 592.60  595.30  591.95  594.85  190721
2   2015-02-02 09:59:00 595.00  595.40  593.35  594.05  125858
3   2015-02-02 10:14:00 594.05  594.65  593.00  593.60  73754
4   2015-02-02 10:29:00 593.40  595.00  592.00  594.20  129346
... ... ... ... ... ... ...
55875   2024-02-22 13:29:00 1085.60 1087.00 1084.90 1085.65 118470
55876   2024-02-22 13:44:00 1085.65 1093.80 1085.00 1093.55 305276
55877   2024-02-22 13:59:00 1093.55 1098.95 1093.45 1096.25 498630
55878   2024-02-22 14:14:00 1096.20 1098.90 1092.55 1093.15 278390
55879   2024-02-22 14:26:00 1093.05 1095.90 1090.65 1093.55 299010

I am able to calculate the rank for the complete dataframe but not as rolling of the last 10 periods

Here is the code I tried
raw_bn['Time'] = raw_bn['Date_time'].dt.time raw_bn['Rank'] = raw_bn.groupby('Time',as_index=False)['Volume'].rank(ascending=False)

New contributor

aditya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Khám phá các thẻ bài đăng