Skip to content

Commit 9de95c1

Browse files
Tejaswini VTejaswini V
authored andcommitted
BUG: rank with object dtype and small values #62036
new change new change
1 parent 7bfef3b commit 9de95c1

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

pandas/_libs/algos.pyx

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,6 @@ tiebreakers = {
7171
"dense": TIEBREAK_DENSE,
7272
}
7373

74-
75-
cdef bint are_diff(object left, object right):
76-
try:
77-
return fabs(left - right) > FP_ERR
78-
except TypeError:
79-
return left != right
80-
81-
8274
class Infinity:
8375
"""
8476
Provide a positive Infinity comparison method for ranking.
@@ -1135,11 +1127,7 @@ cdef void rank_sorted_1d(
11351127
dups += 1
11361128
sum_ranks += i - grp_start + 1
11371129

1138-
if numeric_object_t is object:
1139-
next_val_diff = at_end or are_diff(masked_vals[sort_indexer[i]],
1140-
masked_vals[sort_indexer[i+1]])
1141-
else:
1142-
next_val_diff = at_end or (masked_vals[sort_indexer[i]]
1130+
next_val_diff = at_end or (masked_vals[sort_indexer[i]]
11431131
!= masked_vals[sort_indexer[i+1]])
11441132

11451133
# We'll need this check later anyway to determine group size, so just

pandas/tests/test_algos.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
IntervalIndex,
3232
MultiIndex,
3333
NaT,
34+
NA,
3435
Period,
3536
PeriodIndex,
3637
Series,
@@ -1809,6 +1810,13 @@ def test_uint64_overflow(self, dtype):
18091810
s = Series([1, 2**63], dtype=dtype)
18101811
tm.assert_numpy_array_equal(algos.rank(s), exp)
18111812

1813+
@pytest.mark.parametrize("method", ["average", "min", "max"])
1814+
def test_object_dtype(self, method):
1815+
exp = np.array([4.0, 1.0, 3.0, np.nan, 2.0], dtype=np.float64)
1816+
s = Series([5.4954145e29, -9.791984e-21, 9.3715776e-26, pd.NA, 1.8790257e-28], dtype="Float64")
1817+
s = s.astype(object)
1818+
tm.assert_numpy_array_equal(algos.rank(s, method=method), exp)
1819+
18121820
def test_too_many_ndims(self):
18131821
arr = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]])
18141822
msg = "Array with ndim > 2 are not supported"

0 commit comments

Comments
 (0)