Skip to content

Commit 9f5803a

Browse files
authored
Merge pull request #3 from CSCD01-team14/bugs/pandas-dev#32572
Bug pandas-dev#32572 fix
2 parents 4c0727e + 8f95910 commit 9f5803a

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

pandas/_libs/algos.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,15 +1050,15 @@ def rank_2d(rank_t[:, :] in_arr, axis=0, ties_method='average',
10501050
if rank_t is object:
10511051
nan_value = Infinity()
10521052
elif rank_t is float64_t:
1053-
nan_value = np.inf
1053+
nan_value = NaN
10541054
elif rank_t is int64_t:
10551055
nan_value = np.iinfo(np.int64).max
10561056

10571057
else:
10581058
if rank_t is object:
10591059
nan_value = NegInfinity()
10601060
elif rank_t is float64_t:
1061-
nan_value = -np.inf
1061+
nan_value = NaN
10621062
elif rank_t is int64_t:
10631063
nan_value = NPY_NAT
10641064

@@ -1120,7 +1120,7 @@ def rank_2d(rank_t[:, :] in_arr, axis=0, ties_method='average',
11201120
if rank_t is object:
11211121
skip_condition = (val is nan_value) and keep_na
11221122
else:
1123-
skip_condition = (val == nan_value) and keep_na
1123+
skip_condition = (val == nan_value or (np.isnan(val) and np.isnan(nan_value))) and keep_na
11241124
if skip_condition:
11251125
ranks[i, argsorted[i, j]] = NaN
11261126

pandas/tests/frame/methods/test_rank.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,27 @@ def test_pct_max_many_rows(self):
329329
)
330330
result = df.rank(pct=True).max()
331331
assert (result == 1).all()
332+
333+
def test_rank_minus_inf_keep_nan(self):
334+
# GH 32593
335+
expected_df = DataFrame({'col': np.array([2.0, 4.0, np.nan, 3.0, 1.0])})
336+
result_df = DataFrame({'col': np.array([1, np.inf, np.nan, 10, -np.inf])}).rank(na_option='keep')
337+
tm.assert_frame_equal(expected_df, result_df)
338+
339+
def test_rank_inf_keep_nan(self):
340+
# GH 32593
341+
expected_df = DataFrame({'col': np.array([1.0, 2.0, 3.0])})
342+
result_df = DataFrame({'col': np.array([-np.inf, 0, np.inf])}).rank(na_option='keep')
343+
tm.assert_frame_equal(expected_df, result_df)
344+
345+
def test_rank_inf_bottom_nan(self):
346+
# GH 32593
347+
expected_df = DataFrame({'col': np.array([1.0, 2.0, 4.0, 3.0])})
348+
result_df = DataFrame({'col': np.array([-np.inf, 0, np.nan, np.inf])}).rank(na_option='bottom')
349+
tm.assert_frame_equal(expected_df, result_df)
350+
351+
def test_rank_inf_decimal_nan(self):
352+
# GH 32593
353+
expected_df = DataFrame({'col': np.array([2.0,3.0,4.5,6.0,1.0,4.5])})
354+
result_df = DataFrame({'col': np.array([5.5, 6.99, np.inf, np.nan, 0.7, np.inf])}).rank(na_option='bottom')
355+
tm.assert_frame_equal(expected_df, result_df)

0 commit comments

Comments
 (0)