Skip to content

Series.sort_values using mergesort and descending is not stable #28697

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

Closed
has2k1 opened this issue Sep 30, 2019 · 2 comments · Fixed by #41712
Closed

Series.sort_values using mergesort and descending is not stable #28697

has2k1 opened this issue Sep 30, 2019 · 2 comments · Fixed by #41712
Assignees
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff good first issue Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@has2k1
Copy link
Contributor

has2k1 commented Sep 30, 2019

Code Sample

import pandas as pd

s = pd.Series([1, 2, 1, 3], ['first', 'b', 'second', 'c'])
print(s.sort_values(ascending=False, kind='mergesort'))

Output

c         3
b         2
second    1
first     1
dtype: int64

Expected Output

c         3
b         2
first     1
second    1
dtype: int64

Problem description

Mergesort should be stable irrespective of direction, but ascending=False (which is not stable) is just the reverse of ascending=True (which is stable).

has2k1 added a commit to has2k1/pandas that referenced this issue Sep 30, 2019
The solution uses the `nargsort` function, which was created to handle
in one place the intricacies of sorting. Not adapting Series.sort_values
to use may have been an oversight.

closes pandas-dev#28697
has2k1 added a commit to has2k1/pandas that referenced this issue Sep 30, 2019
The solution uses the `nargsort` function, which was created to handle
in one place the intricacies of sorting. Not adapting Series.sort_values
to use may have been an oversight.

closes pandas-dev#28697
has2k1 added a commit to has2k1/pandas that referenced this issue Oct 28, 2019
The solution uses the `nargsort` function, which was created to handle
in one place the intricacies of sorting. Not adapting Series.sort_values
to use may have been an oversight.

closes pandas-dev#28697
@mroeschke mroeschke added Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug labels Jan 27, 2020
@mzeitlin11
Copy link
Member

This looks to be fixed in master. Looks like there are tests for the dataframe case, but could use tests for the series case.

@mzeitlin11 mzeitlin11 added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug labels Mar 29, 2021
@na2shell
Copy link
Contributor

na2shell commented Apr 9, 2021

take

@mroeschke mroeschke mentioned this issue May 29, 2021
15 tasks
@mroeschke mroeschke added this to the 1.3 milestone May 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
4 participants