diff --git a/pandas/tests/frame/methods/test_rank.py b/pandas/tests/frame/methods/test_rank.py index 991a91275ae1d..6ad1b475e28a2 100644 --- a/pandas/tests/frame/methods/test_rank.py +++ b/pandas/tests/frame/methods/test_rank.py @@ -392,7 +392,7 @@ def test_pct_max_many_rows(self): ([NegInfinity(), "1", "A", "BA", "Ba", "C", Infinity()], "object"), ], ) - def test_rank_inf_and_nan(self, contents, dtype): + def test_rank_inf_and_nan(self, contents, dtype, frame_or_series): dtype_na_map = { "float64": np.nan, "float32": np.nan, @@ -410,12 +410,13 @@ def test_rank_inf_and_nan(self, contents, dtype): nan_indices = np.random.choice(range(len(values)), 5) values = np.insert(values, nan_indices, na_value) exp_order = np.insert(exp_order, nan_indices, np.nan) - # shuffle the testing array and expected results in the same way + + # Shuffle the testing array and expected results in the same way random_order = np.random.permutation(len(values)) - df = DataFrame({"a": values[random_order]}) - expected = DataFrame({"a": exp_order[random_order]}, dtype="float64") - result = df.rank() - tm.assert_frame_equal(result, expected) + obj = frame_or_series(values[random_order]) + expected = frame_or_series(exp_order[random_order], dtype="float64") + result = obj.rank() + tm.assert_equal(result, expected) def test_df_series_inf_nan_consistency(self): # GH#32593 diff --git a/pandas/tests/series/methods/test_rank.py b/pandas/tests/series/methods/test_rank.py index 6d3c37659f5c4..9d052e2236aae 100644 --- a/pandas/tests/series/methods/test_rank.py +++ b/pandas/tests/series/methods/test_rank.py @@ -3,7 +3,6 @@ import numpy as np import pytest -from pandas._libs import iNaT from pandas._libs.algos import Infinity, NegInfinity import pandas.util._test_decorators as td @@ -206,91 +205,6 @@ def test_rank_signature(self): with pytest.raises(ValueError, match=msg): s.rank("average") - @pytest.mark.parametrize( - "contents,dtype", - [ - ( - [ - -np.inf, - -50, - -1, - -1e-20, - -1e-25, - -1e-50, - 0, - 1e-40, - 1e-20, - 1e-10, - 2, - 40, - np.inf, - ], - "float64", - ), - ( - [ - -np.inf, - -50, - -1, - -1e-20, - -1e-25, - -1e-45, - 0, - 1e-40, - 1e-20, - 1e-10, - 2, - 40, - np.inf, - ], - "float32", - ), - ([np.iinfo(np.uint8).min, 1, 2, 100, np.iinfo(np.uint8).max], "uint8"), - pytest.param( - [ - np.iinfo(np.int64).min, - -100, - 0, - 1, - 9999, - 100000, - 1e10, - np.iinfo(np.int64).max, - ], - "int64", - marks=pytest.mark.xfail( - reason="iNaT is equivalent to minimum value of dtype" - "int64 pending issue GH#16674" - ), - ), - ([NegInfinity(), "1", "A", "BA", "Ba", "C", Infinity()], "object"), - ], - ) - def test_rank_inf(self, contents, dtype): - dtype_na_map = { - "float64": np.nan, - "float32": np.nan, - "int64": iNaT, - "object": None, - } - # Insert nans at random positions if underlying dtype has missing - # value. Then adjust the expected order by adding nans accordingly - # This is for testing whether rank calculation is affected - # when values are interwined with nan values. - values = np.array(contents, dtype=dtype) - exp_order = np.array(range(len(values)), dtype="float64") + 1.0 - if dtype in dtype_na_map: - na_value = dtype_na_map[dtype] - nan_indices = np.random.choice(range(len(values)), 5) - values = np.insert(values, nan_indices, na_value) - exp_order = np.insert(exp_order, nan_indices, np.nan) - # shuffle the testing array and expected results in the same way - random_order = np.random.permutation(len(values)) - iseries = Series(values[random_order]) - exp = Series(exp_order[random_order], dtype="float64") - iranks = iseries.rank() - tm.assert_series_equal(iranks, exp) - def test_rank_tie_methods(self): s = self.s