-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Rank categorical #15422
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
Closed
Closed
Rank categorical #15422
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
85b267a
Added support for categorical datatype in rank - issue#15420
jeet63 ce90207
BUG: GH#15420 rank for categoricals
jeet63 bf4e36c
GH#15420 added support for na_option when ranking categorical
jeet63 6b70921
GH#15420 move rank inside categoricals
jeet63 4220e56
BUG: GH15420 - _rank private method on Categorical
jeet63 9a6b5cd
BUG: GH15420 - _rank private method on Categorical
jeet63 fa0b4c2
BUG: GH15420 - _rank private method on Categorical
jeet63 fbaba1b
return values for rank from categorical object
jeet63 ef999c3
merged with upstream master
jeet63 5e5bbeb
BUG: GH#15420 rank for categoricals
jeet63 049c0fc
GH#15420 added support for na_option when ranking categorical
jeet63 40d88c1
return values for rank from categorical object
jeet63 f8ec019
ask Categorical for ranking function
jeet63 c43a029
using if/else construct to pick sorting function for categoricals
jeet63 3ba4e3a
corrections after rebasing
jeet63 a7e573b
moved test for categorical, in rank, to top
jeet63 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -578,6 +578,7 @@ Bug Fixes | |
|
||
|
||
|
||
- Bug in ``.rank()`` which incorrectly ranks ordered categories (:issue:`15420`) | ||
|
||
|
||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1057,6 +1057,84 @@ def test_rank(self): | |
iranks = iseries.rank() | ||
assert_series_equal(iranks, exp) | ||
|
||
def test_rank_categorical(self): | ||
# GH issue #15420 rank incorrectly orders ordered categories | ||
|
||
# Test ascending/descending ranking for ordered categoricals | ||
exp = pd.Series([1., 2., 3., 4., 5., 6.]) | ||
exp_desc = pd.Series([6., 5., 4., 3., 2., 1.]) | ||
ordered = pd.Series( | ||
['first', 'second', 'third', 'fourth', 'fifth', 'sixth'] | ||
).astype('category', ).cat.set_categories( | ||
['first', 'second', 'third', 'fourth', 'fifth', 'sixth'], | ||
ordered=True | ||
) | ||
assert_series_equal(ordered.rank(), exp) | ||
assert_series_equal(ordered.rank(ascending=False), exp_desc) | ||
|
||
# Unordered categoricals should be ranked as objects | ||
unordered = pd.Series( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. write down the expected result here and use it in the comparison |
||
['first', 'second', 'third', 'fourth', 'fifth', 'sixth'], | ||
).astype('category').cat.set_categories( | ||
['first', 'second', 'third', 'fourth', 'fifth', 'sixth'], | ||
ordered=False | ||
) | ||
exp_unordered = pd.Series([2., 4., 6., 3., 1., 5.]) | ||
res = unordered.rank() | ||
assert_series_equal(res, exp_unordered) | ||
|
||
# Test na_option for rank data | ||
na_ser = pd.Series( | ||
['first', 'second', 'third', 'fourth', 'fifth', 'sixth', np.NaN] | ||
).astype('category', ).cat.set_categories( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can pass |
||
[ | ||
'first', 'second', 'third', 'fourth', | ||
'fifth', 'sixth', 'seventh' | ||
], | ||
ordered=True | ||
) | ||
|
||
exp_top = pd.Series([2., 3., 4., 5., 6., 7., 1.]) | ||
exp_bot = pd.Series([1., 2., 3., 4., 5., 6., 7.]) | ||
exp_keep = pd.Series([1., 2., 3., 4., 5., 6., np.NaN]) | ||
|
||
assert_series_equal(na_ser.rank(na_option='top'), exp_top) | ||
assert_series_equal(na_ser.rank(na_option='bottom'), exp_bot) | ||
assert_series_equal(na_ser.rank(na_option='keep'), exp_keep) | ||
|
||
# Test na_option for rank data with ascending False | ||
exp_top = pd.Series([7., 6., 5., 4., 3., 2., 1.]) | ||
exp_bot = pd.Series([6., 5., 4., 3., 2., 1., 7.]) | ||
exp_keep = pd.Series([6., 5., 4., 3., 2., 1., np.NaN]) | ||
|
||
assert_series_equal( | ||
na_ser.rank(na_option='top', ascending=False), | ||
exp_top | ||
) | ||
assert_series_equal( | ||
na_ser.rank(na_option='bottom', ascending=False), | ||
exp_bot | ||
) | ||
assert_series_equal( | ||
na_ser.rank(na_option='keep', ascending=False), | ||
exp_keep | ||
) | ||
|
||
# Test with pct=True | ||
na_ser = pd.Series( | ||
['first', 'second', 'third', 'fourth', np.NaN], | ||
).astype('category').cat.set_categories( | ||
['first', 'second', 'third', 'fourth'], | ||
ordered=True | ||
) | ||
exp_top = pd.Series([0.4, 0.6, 0.8, 1., 0.2]) | ||
exp_bot = pd.Series([0.2, 0.4, 0.6, 0.8, 1.]) | ||
exp_keep = pd.Series([0.25, 0.5, 0.75, 1., np.NaN]) | ||
|
||
assert_series_equal(na_ser.rank(na_option='top', pct=True), exp_top) | ||
assert_series_equal(na_ser.rank(na_option='bottom', pct=True), exp_bot) | ||
assert_series_equal(na_ser.rank(na_option='keep', pct=True), exp_keep) | ||
|
||
def test_rank_signature(self): | ||
s = Series([0, 1]) | ||
s.rank(method='average') | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as I said before, I think it can be faster to actually reorder the categories (so rank can use the integer/float codes) instead of passing an object array to rank (or at least check whether the categories are sorted, and in such case also pass the codes).
But given that this is also the current situation, it's not a blocker for this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're interested, that can in a follow-up PR (to get this one merged)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, @jorisvandenbossche didn't see that comment. yes could be an easy followup, something like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jorisvandenbossche sure. Will do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the issue #15498 for this