I'm trying to calculate the rank of a column value within a rolling window in Pandas like this:
df = pd.DataFrame( [[1, 10],
[2, 20],
[3, 50],
[4, 30],
[5, 40]],
columns=['order_col', 'rank_col'])
df['rank'] = df.rolling(3, center=True, min_periods=1, on='order_col')['rank_col'].rank()
The result of rank() though gives the rank of the last row in the window not the one in the centre, as expected:
Any ideas how I can get the rank of the correct row? I.e. I expect the ranks to be 1, 2, 3, 1, 2
EDIT: I chose a small example to illustrate the problem but in actuality my dataframe has thousands of rows and the rolling window is of size 100+ rows.
