diff --git a/pandas/_libs/algos.pyx b/pandas/_libs/algos.pyx index b7f17aee35a44..c0e32c9b7e05d 100644 --- a/pandas/_libs/algos.pyx +++ b/pandas/_libs/algos.pyx @@ -1050,7 +1050,7 @@ 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 @@ -1058,7 +1058,7 @@ def rank_2d(rank_t[:, :] in_arr, axis=0, ties_method='average', 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 @@ -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 diff --git a/pandas/tests/frame/methods/test_rank.py b/pandas/tests/frame/methods/test_rank.py index bab2db3192b4a..9b55e7ff7ed99 100644 --- a/pandas/tests/frame/methods/test_rank.py +++ b/pandas/tests/frame/methods/test_rank.py @@ -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) \ No newline at end of file