Skip to content

Commit 5708b24

Browse files
committed
BUG: Sereis.nlargest thinks True < False
- use 1-arr insteat of -arr for sorting a boolean array casted to uint - add test - add whatsnew
1 parent c79b7bb commit 5708b24

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ Other
416416

417417
- Improved :class:`Timestamp` type checking in various datetime functions to prevent exceptions when using a subclassed `datetime` (:issue:`25851`)
418418
- Bug in :class:`Series` and :class:`DataFrame` repr where ``np.datetime64('NaT')`` and ``np.timedelta64('NaT')`` with ``dtype=object`` would be represented as ``NaN`` (:issue:`25445`)
419-
-
419+
- Bug in :meth:`Series.nlargest` treats ``True`` as smaller than ``False`` (:issue:`26154`)
420420
-
421421

422422

pandas/core/algorithms.py

+4
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,10 @@ def compute(self, method):
11181118
# GH 21426: ensure reverse ordering at boundaries
11191119
arr -= 1
11201120

1121+
if is_bool_dtype(pandas_dtype):
1122+
# GH 26154: ensure False is smaller than True
1123+
arr = 1 - (-arr)
1124+
11211125
if self.keep == 'last':
11221126
arr = arr[::-1]
11231127

pandas/tests/series/test_analytics.py

+6
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,12 @@ def test_duplicate_keep_all_ties(self):
13301330
expected = Series([6, 7, 7, 7, 7], index=[7, 3, 4, 5, 6])
13311331
assert_series_equal(result, expected)
13321332

1333+
def test_boolean(self):
1334+
s = Series([True, False])
1335+
result = s.nlargest(1)
1336+
expected = s.sort_values(ascending=False).head(1)
1337+
assert_series_equal(result, expected)
1338+
13331339

13341340
class TestCategoricalSeriesAnalytics:
13351341

0 commit comments

Comments
 (0)