Skip to content

BUG: Series.nlargest thinks True < False #26157

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

Merged
merged 1 commit into from
Apr 23, 2019
Merged
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/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^
Expand All @@ -415,7 +416,6 @@ Other
^^^^^

-
-


.. _whatsnew_0.250.contributors:
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down