Skip to content

Bug #32593 fix #3

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 3 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pandas/_libs/algos.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1050,15 +1050,15 @@ def rank_2d(rank_t[:, :] in_arr, axis=0, ties_method='average',
if rank_t is object:
nan_value = Infinity()
elif rank_t is float64_t:
nan_value = np.inf
nan_value = NaN
elif rank_t is int64_t:
nan_value = np.iinfo(np.int64).max

else:
if rank_t is object:
nan_value = NegInfinity()
elif rank_t is float64_t:
nan_value = -np.inf
nan_value = NaN
elif rank_t is int64_t:
nan_value = NPY_NAT

Expand Down Expand Up @@ -1120,7 +1120,7 @@ def rank_2d(rank_t[:, :] in_arr, axis=0, ties_method='average',
if rank_t is object:
skip_condition = (val is nan_value) and keep_na
else:
skip_condition = (val == nan_value) and keep_na
skip_condition = (val == nan_value or (np.isnan(val) and np.isnan(nan_value))) and keep_na
if skip_condition:
ranks[i, argsorted[i, j]] = NaN

Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/frame/methods/test_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,27 @@ def test_pct_max_many_rows(self):
)
result = df.rank(pct=True).max()
assert (result == 1).all()

def test_rank_minus_inf_keep_nan(self):
# GH 32593
expected_df = DataFrame({'col': np.array([2.0, 4.0, np.nan, 3.0, 1.0])})
result_df = DataFrame({'col': np.array([1, np.inf, np.nan, 10, -np.inf])}).rank(na_option='keep')
tm.assert_frame_equal(expected_df, result_df)

def test_rank_inf_keep_nan(self):
# GH 32593
expected_df = DataFrame({'col': np.array([1.0, 2.0, 3.0])})
result_df = DataFrame({'col': np.array([-np.inf, 0, np.inf])}).rank(na_option='keep')
tm.assert_frame_equal(expected_df, result_df)

def test_rank_inf_bottom_nan(self):
# GH 32593
expected_df = DataFrame({'col': np.array([1.0, 2.0, 4.0, 3.0])})
result_df = DataFrame({'col': np.array([-np.inf, 0, np.nan, np.inf])}).rank(na_option='bottom')
tm.assert_frame_equal(expected_df, result_df)

def test_rank_inf_decimal_nan(self):
# GH 32593
expected_df = DataFrame({'col': np.array([2.0,3.0,4.5,6.0,1.0,4.5])})
result_df = DataFrame({'col': np.array([5.5, 6.99, np.inf, np.nan, 0.7, np.inf])}).rank(na_option='bottom')
tm.assert_frame_equal(expected_df, result_df)