Skip to content

Commit a13ef66

Browse files
swyoonjreback
authored andcommitted
BUG: Sereis.nlargest thinks True < False (#26157)
1 parent fdda543 commit a13ef66

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ Reshaping
402402
- Bug in :func:`merge` where merging with equivalent Categorical dtypes was raising an error (:issue:`22501`)
403403
- Bug in :class:`DataFrame` constructor when passing non-empty tuples would cause a segmentation fault (:issue:`25691`)
404404
- Bug in :func:`pandas.cut` where large bins could incorrectly raise an error due to an integer overflow (:issue:`26045`)
405+
- Bug in :meth:`Series.nlargest` treats ``True`` as smaller than ``False`` (:issue:`26154`)
405406

406407
Sparse
407408
^^^^^^
@@ -415,7 +416,6 @@ Other
415416
^^^^^
416417

417418
-
418-
-
419419

420420

421421
.. _whatsnew_0.250.contributors:

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+
elif 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

+10
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,16 @@ 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+
@pytest.mark.parametrize('data,expected',
1334+
[([True, False], [True]),
1335+
([True, False, True, True], [True])])
1336+
def test_boolean(self, data, expected):
1337+
# GH 26154 : ensure True > False
1338+
s = Series(data)
1339+
result = s.nlargest(1)
1340+
expected = Series(expected)
1341+
assert_series_equal(result, expected)
1342+
13331343

13341344
class TestCategoricalSeriesAnalytics:
13351345

0 commit comments

Comments
 (0)