-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
FIX: add support for desc order when ranking infs with nans #19538 #20091
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9a6a4b9
fix rank issue when asec is false
peterpanmj dba2258
fix the non_na_idx
peterpanmj 5eb57b0
add test ranking inf/nan in descending order
peterpanmj 1622515
fix some style errors in test_rank
peterpanmj f6d0fd0
parametrize test_rank_tie_methods_on_infs_nans, add a small test for …
peterpanmj 45f60fe
Merge branch 'master' into rank_desc
peterpanmj 8b0467f
add issue number and move expected results into comparing method
peterpanmj b503743
fix some pep8 errors
peterpanmj 62e96f6
update whatsnew
peterpanmj 9e47c0b
Merge branch 'master' into PR_TOOL_MERGE_PR_20091
jreback 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
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 |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
from pandas.tests.series.common import TestData | ||
from pandas._libs.tslib import iNaT | ||
from pandas._libs.algos import Infinity, NegInfinity | ||
from itertools import chain | ||
import pandas.util._test_decorators as td | ||
|
||
|
||
class TestSeriesRank(TestData): | ||
|
@@ -257,38 +259,52 @@ def _check(s, expected, method='average'): | |
series = s if dtype is None else s.astype(dtype) | ||
_check(series, results[method], method=method) | ||
|
||
def test_rank_tie_methods_on_infs_nans(self): | ||
@td.skip_if_no_scipy | ||
@pytest.mark.parametrize('ascending', [True, False]) | ||
@pytest.mark.parametrize('method', ['average', 'min', 'max', 'first', | ||
'dense']) | ||
@pytest.mark.parametrize('na_option', ['top', 'bottom', 'keep']) | ||
def test_rank_tie_methods_on_infs_nans(self, method, na_option, ascending): | ||
dtypes = [('object', None, Infinity(), NegInfinity()), | ||
('float64', np.nan, np.inf, -np.inf)] | ||
chunk = 3 | ||
disabled = set([('object', 'first')]) | ||
|
||
def _check(s, expected, method='average', na_option='keep'): | ||
result = s.rank(method=method, na_option=na_option) | ||
def _check(s, method, na_option, ascending): | ||
exp_ranks = { | ||
'average': ([2, 2, 2], [5, 5, 5], [8, 8, 8]), | ||
'min': ([1, 1, 1], [4, 4, 4], [7, 7, 7]), | ||
'max': ([3, 3, 3], [6, 6, 6], [9, 9, 9]), | ||
'first': ([1, 2, 3], [4, 5, 6], [7, 8, 9]), | ||
'dense': ([1, 1, 1], [2, 2, 2], [3, 3, 3]) | ||
} | ||
ranks = exp_ranks[method] | ||
if na_option == 'top': | ||
order = [ranks[1], ranks[0], ranks[2]] | ||
elif na_option == 'bottom': | ||
order = [ranks[0], ranks[2], ranks[1]] | ||
else: | ||
order = [ranks[0], [np.nan] * chunk, ranks[1]] | ||
expected = order if ascending else order[::-1] | ||
expected = list(chain.from_iterable(expected)) | ||
result = s.rank(method=method, na_option=na_option, | ||
ascending=ascending) | ||
tm.assert_series_equal(result, Series(expected, dtype='float64')) | ||
|
||
exp_ranks = { | ||
'average': ([2, 2, 2], [5, 5, 5], [8, 8, 8]), | ||
'min': ([1, 1, 1], [4, 4, 4], [7, 7, 7]), | ||
'max': ([3, 3, 3], [6, 6, 6], [9, 9, 9]), | ||
'first': ([1, 2, 3], [4, 5, 6], [7, 8, 9]), | ||
'dense': ([1, 1, 1], [2, 2, 2], [3, 3, 3]) | ||
} | ||
na_options = ('top', 'bottom', 'keep') | ||
for dtype, na_value, pos_inf, neg_inf in dtypes: | ||
in_arr = [neg_inf] * chunk + [na_value] * chunk + [pos_inf] * chunk | ||
iseries = Series(in_arr, dtype=dtype) | ||
for method, na_opt in product(exp_ranks.keys(), na_options): | ||
ranks = exp_ranks[method] | ||
if (dtype, method) in disabled: | ||
continue | ||
if na_opt == 'top': | ||
order = ranks[1] + ranks[0] + ranks[2] | ||
elif na_opt == 'bottom': | ||
order = ranks[0] + ranks[2] + ranks[1] | ||
else: | ||
order = ranks[0] + [np.nan] * chunk + ranks[1] | ||
_check(iseries, order, method, na_opt) | ||
if (dtype, method) in disabled: | ||
continue | ||
_check(iseries, method, na_option, ascending) | ||
|
||
def test_rank_desc_mix_nans_infs(self): | ||
# GH 19538 | ||
# check descending ranking when mix nans and infs | ||
iseries = Series([1, np.nan, np.inf, -np.inf, 25]) | ||
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. can you add the issue number |
||
result = iseries.rank(ascending=False) | ||
exp = Series([3, np.nan, 1, 4, 2], dtype='float64') | ||
tm.assert_series_equal(result, exp) | ||
|
||
def test_rank_methods_series(self): | ||
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. ca you change this to use the |
||
pytest.importorskip('scipy.stats.special') | ||
|
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.
can you inline these below