Skip to content

Commit 95ab36d

Browse files
authored
Fix rank method with nullable int (#57779)
* Fix rank method with nullable int * Add whatsnew note
1 parent 7debc1f commit 95ab36d

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

Diff for: doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ Bug fixes
282282
- Fixed bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`)
283283
- Fixed bug in :meth:`DataFrame.update` bool dtype being converted to object (:issue:`55509`)
284284
- Fixed bug in :meth:`Series.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`)
285+
- Fixed bug in :meth:`Series.rank` that doesn't preserve missing values for nullable integers when ``na_option='keep'``. (:issue:`56976`)
285286

286287
Categorical
287288
^^^^^^^^^^^

Diff for: pandas/core/arrays/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2206,7 +2206,7 @@ def _rank(
22062206
raise NotImplementedError
22072207

22082208
return rank(
2209-
self._values_for_argsort(),
2209+
self,
22102210
axis=axis,
22112211
method=method,
22122212
na_option=na_option,

Diff for: pandas/tests/series/methods/test_rank.py

+10
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,16 @@ def test_rank_categorical(self):
234234
tm.assert_series_equal(na_ser.rank(na_option="bottom", pct=True), exp_bot)
235235
tm.assert_series_equal(na_ser.rank(na_option="keep", pct=True), exp_keep)
236236

237+
def test_rank_nullable_integer(self):
238+
# GH 56976
239+
exp = Series([np.nan, 2, np.nan, 3, 3, 2, 3, 1])
240+
exp = exp.astype("Int64")
241+
result = exp.rank(na_option="keep")
242+
243+
expected = Series([np.nan, 2.5, np.nan, 5.0, 5.0, 2.5, 5.0, 1.0])
244+
245+
tm.assert_series_equal(result, expected)
246+
237247
def test_rank_signature(self):
238248
s = Series([0, 1])
239249
s.rank(method="average")

0 commit comments

Comments
 (0)