Skip to content

Commit f2a8cd0

Browse files
committed
Check if NPY_NAT is NA for int64 in rank() (pandas-dev#32859)
1 parent a0c8425 commit f2a8cd0

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ ExtensionArray
157157

158158
Other
159159
^^^^^
160-
-
160+
- Bug in :meth:`Series.rank` incorrectly treating int64 min value as NaN (:issue:`32859`)
161161
-
162162

163163
.. ---------------------------------------------------------------------------

pandas/_libs/algos.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ def rank_1d(
840840
elif rank_t is float64_t:
841841
mask = np.isnan(values)
842842
elif rank_t is int64_t:
843-
mask = values == NPY_NAT
843+
mask = missing.isnaobj(values)
844844

845845
# create copy in case of NPY_NAT
846846
# values are mutated inplace

pandas/tests/test_algos.py

+13
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,19 @@ def test_basic(self):
17691769
s = Series([1, 100], dtype=dtype)
17701770
tm.assert_numpy_array_equal(algos.rank(s), exp)
17711771

1772+
@pytest.mark.parametrize("dtype", ["int32", "int64"])
1773+
def test_negative_min_rank(self, dtype):
1774+
# GH#32859
1775+
# Check that nan is respected on float64
1776+
s = pd.Series(np.array([np.inf, np.nan, -np.inf]))
1777+
expected = pd.Series(np.array([2.0, np.nan, 1.0]))
1778+
tm.assert_series_equal(s.rank(na_option="keep"), expected)
1779+
1780+
# Rank works if coverted to most negative value
1781+
s = pd.Series(np.array([np.inf, np.nan, -np.inf]).astype(dtype))
1782+
expected = pd.Series(np.array([2.0, 2.0, 2.0]))
1783+
tm.assert_series_equal(s.rank(na_option="keep"), expected)
1784+
17721785
def test_uint64_overflow(self):
17731786
exp = np.array([1, 2], dtype=np.float64)
17741787

0 commit comments

Comments
 (0)