Skip to content

BUG: Fix Series.sort_values descending & mergesort #28698

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
wants to merge 1 commit into from

Conversation

has2k1
Copy link
Contributor

@has2k1 has2k1 commented 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.

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it’s possible some existing test are off because this path is not exactly the same as nanargsort (but we do want to unify)

@@ -86,6 +86,12 @@ def test_sort_values(self):
with pytest.raises(ValueError, match=msg):
s.sort_values(inplace=True)

# mergesort and ascending=False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make a new test

@@ -102,6 +102,9 @@ Other
- Compatibility with Python 3.8 in :meth:`DataFrame.query` (:issue:`27261`)
- Fix to ensure that tab-completion in an IPython console does not raise
warnings for deprecated attributes (:issue:`27900`).
- Bug in :meth:`Series.sort_values` when ``ascending`` is set to ``False`` and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to 1.0 this is a non trivial change

@has2k1
Copy link
Contributor Author

has2k1 commented Oct 1, 2019

it’s possible some existing test are off because this path is not exactly the same as nanargsort (but we do want to unify)

Yes that is the case. Now for quicksort, the ascending and descending sort are not guaranteed to be reversed versions of each other.

n = 100000
np.random.seed(123)
values = np.random.randint(0, 4, n)
idx = np.arange(n)
s = pd.Series(values, index=idx)
asc = s.sort_values(ascending=True)
desc = s.sort_values(ascending=False)
asc.index.equals(desc.index[::-1])  # False ( without nanargsort it is True)

Many tests for Series.value_counts -- which are failing -- rely on checking the index, which is arbitrary (#24302).

@jbrockmendel
Copy link
Member

@has2k1 can you rebase

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 has2k1 force-pushed the series_sort_values branch from 48fe4cb to ce1d2bf Compare October 28, 2019 20:12
@WillAyd
Copy link
Member

WillAyd commented Dec 9, 2019

Closing as this looks stale, but certainly @has2k1 ping if you'd like to pick back up

@WillAyd WillAyd closed this Dec 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Series.sort_values using mergesort and descending is not stable
4 participants