Skip to content

Check if NPY_NAT is NA for int64 in rank() (#32859) #35533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ ExtensionArray

Other
^^^^^
-
- Bug in :meth:`Series.rank` incorrectly treating int64 min value as NaN (:issue:`32859`)
-

.. ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/algos.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ def rank_1d(
elif rank_t is float64_t:
mask = np.isnan(values)
elif rank_t is int64_t:
mask = values == NPY_NAT
mask = missing.isnaobj(values)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is going to be all-False

We probably need to specifically handle datetimelike case in which the existing check is correct


# create copy in case of NPY_NAT
# values are mutated inplace
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -1769,6 +1769,19 @@ def test_basic(self):
s = Series([1, 100], dtype=dtype)
tm.assert_numpy_array_equal(algos.rank(s), exp)

@pytest.mark.parametrize("dtype", ["int32", "int64"])
def test_negative_min_rank(self, dtype):
# GH#32859
# Check that nan is respected on float64
s = pd.Series(np.array([np.inf, np.nan, -np.inf]))
expected = pd.Series(np.array([2.0, np.nan, 1.0]))
tm.assert_series_equal(s.rank(na_option="keep"), expected)

# Rank works if coverted to most negative value
s = pd.Series(np.array([np.inf, np.nan, -np.inf]).astype(dtype))
expected = pd.Series(np.array([2.0, 2.0, 2.0]))
tm.assert_series_equal(s.rank(na_option="keep"), expected)

def test_uint64_overflow(self):
exp = np.array([1, 2], dtype=np.float64)

Expand Down