diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index accfeee484430..9afcf3ddcdbb1 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -402,6 +402,7 @@ Reshaping - Bug in :func:`merge` where merging with equivalent Categorical dtypes was raising an error (:issue:`22501`) - Bug in :class:`DataFrame` constructor when passing non-empty tuples would cause a segmentation fault (:issue:`25691`) - Bug in :func:`pandas.cut` where large bins could incorrectly raise an error due to an integer overflow (:issue:`26045`) +- Bug in :meth:`Series.nlargest` treats ``True`` as smaller than ``False`` (:issue:`26154`) Sparse ^^^^^^ @@ -415,7 +416,6 @@ Other ^^^^^ - -- .. _whatsnew_0.250.contributors: diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index df40461c96399..cbd27d0f8e4f0 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -1118,6 +1118,10 @@ def compute(self, method): # GH 21426: ensure reverse ordering at boundaries arr -= 1 + elif is_bool_dtype(pandas_dtype): + # GH 26154: ensure False is smaller than True + arr = 1 - (-arr) + if self.keep == 'last': arr = arr[::-1] diff --git a/pandas/tests/series/test_analytics.py b/pandas/tests/series/test_analytics.py index 4a7729dc15671..a12845df18c1c 100644 --- a/pandas/tests/series/test_analytics.py +++ b/pandas/tests/series/test_analytics.py @@ -1330,6 +1330,16 @@ def test_duplicate_keep_all_ties(self): expected = Series([6, 7, 7, 7, 7], index=[7, 3, 4, 5, 6]) assert_series_equal(result, expected) + @pytest.mark.parametrize('data,expected', + [([True, False], [True]), + ([True, False, True, True], [True])]) + def test_boolean(self, data, expected): + # GH 26154 : ensure True > False + s = Series(data) + result = s.nlargest(1) + expected = Series(expected) + assert_series_equal(result, expected) + class TestCategoricalSeriesAnalytics: