Pandas (0.18) Rank: unexpected behavior for method = 'dense' and pct = True
See original GitHub issueI find the behavior of rank function with method = ‘dense’ and pct = True unexpected as it looks like, in order to calculate percentile ranks, the function is using the total number of observations instead of the number of distinct observations.
Code Sample, a copy-pastable example if possible
import pandas as pd
n_rep = 2
ts = pd.Series([1,2,3,4] * n_rep )
output = ts.rank(method = 'dense', pct = True)
Problem description
ts.rank(method = 'dense', pct = True)
Out[116]:
0 0.125
1 0.250
2 0.375
3 0.500
4 0.125
5 0.250
6 0.375
7 0.500
Expected Output
Something similar to:
pd.Series([1,2,3,4] * 2).rank(method = 'dense', pct = True) * n_rep
Out[118]:
0 0.25
1 0.50
2 0.75
3 1.00
4 0.25
5 0.50
6 0.75
7 1.00
Also, I would expected the result above to be invariant to n_rep. i.e. I would expect a “mapping” {value -> pct_rank} that would not depend on how many times the value is repeated, while it is not the case here.
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Pandas Rank: unexpected behavior for method = 'dense' and ...
All that pct=True does is divide by the nobs, which gives unexpected behavior for method = 'dense', so this considered as a bug...
Read more >What's New — pandas 0.18.0 documentation - PyData |
Check the API Changes and deprecations before updating. What's new in v0.18.0. New features. Window functions are now methods; Changes to rename; Range...
Read more >What's new in 0.24.0 (January 25, 2019) - Pandas 中文
Optional Integer NA Support; New APIs for accessing the array backing a Series or Index; A new top-level method for creating arrays; Store ......
Read more >Pandas Rank: unexpected behavior for method = 'dense' and ...
All that pct=True does is divide by the nobs, which gives unexpected behavior for method = 'dense', so this considered as a bug...
Read more >What's New — pandas 0.23.4 documentation
To retain the current behavior, convert the 'datetime.date' to a datetime ... Bug in DataFrame.rank() and Series.rank() when method='dense' and pct=True in ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I agree with @FXLab91 that this is very strange behavior, and I can’t see why anyone would want it. So I would be inclined to treat it as a bug and fix it for the next release.
Restating @dsm054’s question (and asking a few of my own), should all other
method
’s return a “dense percentage” on a 100% basis whenpct=True
?As @dsm054 noted,
Series([1,2,2]).max(method='min', pct=True)
will return [1/3, 2/3, 2/3]. Should this return [1/2, 2/2, 2/2]?Now if method=‘max’,
Series([1,2,2]).max(method='max', pct=True)
will return [1/3, 3/3, 3/3]. Is that is the desired output or should it again be [1/2, 2/2, 2/2]?#15639 will fix the
method='dense'
case, but we need to address othermethod
s as well.