Skip to content

Commit eb53bf7

Browse files
authored
TST/CLN: deduplicate troublesome rank values (#38894)
* WIP * TST/CLN: deduplicate troublesome rank values * Remove unneeded imports * Address comments * Use frame_or_series instead
1 parent 125441c commit eb53bf7

File tree

2 files changed

+7
-92
lines changed

2 files changed

+7
-92
lines changed

pandas/tests/frame/methods/test_rank.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def test_pct_max_many_rows(self):
392392
([NegInfinity(), "1", "A", "BA", "Ba", "C", Infinity()], "object"),
393393
],
394394
)
395-
def test_rank_inf_and_nan(self, contents, dtype):
395+
def test_rank_inf_and_nan(self, contents, dtype, frame_or_series):
396396
dtype_na_map = {
397397
"float64": np.nan,
398398
"float32": np.nan,
@@ -410,12 +410,13 @@ def test_rank_inf_and_nan(self, contents, dtype):
410410
nan_indices = np.random.choice(range(len(values)), 5)
411411
values = np.insert(values, nan_indices, na_value)
412412
exp_order = np.insert(exp_order, nan_indices, np.nan)
413-
# shuffle the testing array and expected results in the same way
413+
414+
# Shuffle the testing array and expected results in the same way
414415
random_order = np.random.permutation(len(values))
415-
df = DataFrame({"a": values[random_order]})
416-
expected = DataFrame({"a": exp_order[random_order]}, dtype="float64")
417-
result = df.rank()
418-
tm.assert_frame_equal(result, expected)
416+
obj = frame_or_series(values[random_order])
417+
expected = frame_or_series(exp_order[random_order], dtype="float64")
418+
result = obj.rank()
419+
tm.assert_equal(result, expected)
419420

420421
def test_df_series_inf_nan_consistency(self):
421422
# GH#32593

pandas/tests/series/methods/test_rank.py

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import numpy as np
44
import pytest
55

6-
from pandas._libs import iNaT
76
from pandas._libs.algos import Infinity, NegInfinity
87
import pandas.util._test_decorators as td
98

@@ -206,91 +205,6 @@ def test_rank_signature(self):
206205
with pytest.raises(ValueError, match=msg):
207206
s.rank("average")
208207

209-
@pytest.mark.parametrize(
210-
"contents,dtype",
211-
[
212-
(
213-
[
214-
-np.inf,
215-
-50,
216-
-1,
217-
-1e-20,
218-
-1e-25,
219-
-1e-50,
220-
0,
221-
1e-40,
222-
1e-20,
223-
1e-10,
224-
2,
225-
40,
226-
np.inf,
227-
],
228-
"float64",
229-
),
230-
(
231-
[
232-
-np.inf,
233-
-50,
234-
-1,
235-
-1e-20,
236-
-1e-25,
237-
-1e-45,
238-
0,
239-
1e-40,
240-
1e-20,
241-
1e-10,
242-
2,
243-
40,
244-
np.inf,
245-
],
246-
"float32",
247-
),
248-
([np.iinfo(np.uint8).min, 1, 2, 100, np.iinfo(np.uint8).max], "uint8"),
249-
pytest.param(
250-
[
251-
np.iinfo(np.int64).min,
252-
-100,
253-
0,
254-
1,
255-
9999,
256-
100000,
257-
1e10,
258-
np.iinfo(np.int64).max,
259-
],
260-
"int64",
261-
marks=pytest.mark.xfail(
262-
reason="iNaT is equivalent to minimum value of dtype"
263-
"int64 pending issue GH#16674"
264-
),
265-
),
266-
([NegInfinity(), "1", "A", "BA", "Ba", "C", Infinity()], "object"),
267-
],
268-
)
269-
def test_rank_inf(self, contents, dtype):
270-
dtype_na_map = {
271-
"float64": np.nan,
272-
"float32": np.nan,
273-
"int64": iNaT,
274-
"object": None,
275-
}
276-
# Insert nans at random positions if underlying dtype has missing
277-
# value. Then adjust the expected order by adding nans accordingly
278-
# This is for testing whether rank calculation is affected
279-
# when values are interwined with nan values.
280-
values = np.array(contents, dtype=dtype)
281-
exp_order = np.array(range(len(values)), dtype="float64") + 1.0
282-
if dtype in dtype_na_map:
283-
na_value = dtype_na_map[dtype]
284-
nan_indices = np.random.choice(range(len(values)), 5)
285-
values = np.insert(values, nan_indices, na_value)
286-
exp_order = np.insert(exp_order, nan_indices, np.nan)
287-
# shuffle the testing array and expected results in the same way
288-
random_order = np.random.permutation(len(values))
289-
iseries = Series(values[random_order])
290-
exp = Series(exp_order[random_order], dtype="float64")
291-
iranks = iseries.rank()
292-
tm.assert_series_equal(iranks, exp)
293-
294208
def test_rank_tie_methods(self):
295209
s = self.s
296210

0 commit comments

Comments
 (0)