Pandas: Rolling Rank issues in pandas

Created on 4 Apr 2018  路  7Comments  路  Source: pandas-dev/pandas

When doing some math works in pandas, the rolling function is very useful, such as pd.rolling().mean() or
pd.rolling().max().
Here is my problem: when I want to get a time-series rank in a Series or a column in DataFrame, there is no function like pd.rolling().rank(). Is there any function can work as pd.rolling().rank() ?

Reshaping Usage Question

Most helpful comment

you can do something like this:

In [8]: series_a.rolling(3).apply(lambda x: Series(x).rank()[0])
Out[8]: 
0    NaN
1    NaN
2    3.0
3    3.0
4    1.0
5    2.0
6    1.0
7    3.0
8    3.0
9    1.0
dtype: float64

duplicate of #9481

All 7 comments

show a specific example

0 2.11
1 0.71
2 -1.07
3 0.09
4 -0.37
5 2.45
6 0.66
7 -1.08
8 -0.65
9 -0.22

like this. I want to use a function like pd.rolling(window=3).rank() and get the result below. Each number is the rank of past three period number.
0 nan
1 nan
2 1
3 2
4 2
5 3
6 2
7 1
8 2
9 3

should we actual copy pastable code for construction -

series_a = pd.Series([2.11,0.71,-1.07,0.09,-0.37,2.45,0.66,-1.08,-0.65,-0.22])

and I don't know the next step to get a time-series rank of the series.

you can do something like this:

In [8]: series_a.rolling(3).apply(lambda x: Series(x).rank()[0])
Out[8]: 
0    NaN
1    NaN
2    3.0
3    3.0
4    1.0
5    2.0
6    1.0
7    3.0
8    3.0
9    1.0
dtype: float64

duplicate of #9481

Thanks bro!

I need to get the last value so series_a.rolling(3).apply(lambda x: Series(x).rank().values[-1])

Was this page helpful?
0 / 5 - 0 ratings