Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rolling Rank issues in pandas #20602

Closed
qirenjie2011 opened this issue Apr 4, 2018 · 7 comments
Closed

Rolling Rank issues in pandas #20602

qirenjie2011 opened this issue Apr 4, 2018 · 7 comments
Labels
Reshaping Concat, Merge/Join, Stack/Unstack, Explode Usage Question

Comments

@qirenjie2011
Copy link

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() ?

@jreback
Copy link
Contributor

jreback commented Apr 4, 2018

show a specific example

@qirenjie2011
Copy link
Author

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

@jreback
Copy link
Contributor

jreback commented Apr 9, 2018

should we actual copy pastable code for construction -

@qirenjie2011
Copy link
Author

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.

@jreback
Copy link
Contributor

jreback commented Apr 9, 2018

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

@jreback jreback closed this as completed Apr 9, 2018
@jreback jreback added Reshaping Concat, Merge/Join, Stack/Unstack, Explode Usage Question labels Apr 9, 2018
@jreback jreback added this to the No action milestone Apr 9, 2018
@qirenjie2011
Copy link
Author

Thanks bro!

@d6tdev
Copy link

d6tdev commented Feb 15, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Reshaping Concat, Merge/Join, Stack/Unstack, Explode Usage Question
Projects
None yet
Development

No branches or pull requests

3 participants