Skip to content

Commit 1ccaaa4

Browse files
committed
Remove special-casing
1 parent aa6c637 commit 1ccaaa4

File tree

2 files changed

+9
-25
lines changed

2 files changed

+9
-25
lines changed

pandas/core/algorithms.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,17 +1065,7 @@ def rank(
10651065
(e.g. 1, 2, 3) or in percentile form (e.g. 0.333..., 0.666..., 1).
10661066
"""
10671067
is_datetimelike = needs_i8_conversion(values.dtype)
1068-
if (
1069-
isinstance(values.dtype, BaseMaskedDtype)
1070-
and values._hasna # type: ignore[union-attr]
1071-
and values.dtype.kind in "iuf"
1072-
):
1073-
# e.g. test_rank_ea_small_values
1074-
# TODO: bug in the object-dtype path that we would get without
1075-
# this special casting.
1076-
values = values.to_numpy(dtype=np.float64, na_value=np.nan) # type: ignore[union-attr]
1077-
else:
1078-
values = _ensure_data(values)
1068+
values = _ensure_data(values)
10791069

10801070
if values.ndim == 1:
10811071
ranks = algos.rank_1d(

pandas/tests/series/methods/test_rank.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -288,20 +288,14 @@ def test_rank_tie_methods(
288288
pytest.skip("int64/str does not support NaN")
289289

290290
ser = ser if dtype is None else ser.astype(dtype)
291+
if dtype in ["float64[pyarrow]", "Float64"] and not using_nan_is_na:
292+
# TODO: use ser.replace(np.nan, NA) once that works
293+
ser[np.isnan(ser.to_numpy(dtype=np.float64, na_value=np.nan))] = NA
294+
mask = np.isnan(exp)
295+
exp = exp.astype(object)
296+
exp[mask] = NA
297+
291298
result = ser.rank(method=method)
292-
if dtype == "float64[pyarrow]" and not using_nan_is_na:
293-
# the NaNs are not treated as NA
294-
exp = exp.copy()
295-
if method == "average":
296-
exp[np.isnan(ser)] = 9.5
297-
elif method == "dense":
298-
exp[np.isnan(ser)] = 6
299-
elif method == "max":
300-
exp[np.isnan(ser)] = 10
301-
elif method == "min":
302-
exp[np.isnan(ser)] = 9
303-
elif method == "first":
304-
exp[np.isnan(ser)] = [9, 10]
305299

306300
if dtype == "string[pyarrow]" and not using_nan_is_na:
307301
mask = np.isnan(exp)
@@ -368,7 +362,7 @@ def test_rank_tie_methods_on_infs_nans(
368362
order = [ranks[1], ranks[0], ranks[2]]
369363
elif na_option == "bottom":
370364
order = [ranks[0], ranks[2], ranks[1]]
371-
elif dtype == "float64[pyarrow]" and not using_nan_is_na:
365+
elif dtype in ("float64[pyarrow]", "Float64") and not using_nan_is_na:
372366
order = [ranks[0], [NA] * chunk, ranks[1]]
373367
else:
374368
order = [ranks[0], [np.nan] * chunk, ranks[1]]

0 commit comments

Comments
 (0)