-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
DOC: Clarify that DataFrame.sort_values is stable for sorting by multiple columns or labels #38426
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
jreback
merged 6 commits into
pandas-dev:master
from
jotasi:clarify_doc_sort_values_kind
Dec 22, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f1171a8
DOC: Clarify that DataFrame.sort_values is stable for len(by)>1
2e24f69
TST: Update test case to show multilabel sort is stable
2addf1d
TST: Parametrize sort options in multicol sort_values test
da6b97a
TST: Simplified parametrization
33c1a93
TST: Removed parametrization IDs
911745a
TST: Added reference to github PR
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -217,26 +217,48 @@ def test_sort_values_stable_descending_sort(self): | |
sorted_df = df.sort_values(by="sort_col", kind="mergesort", ascending=False) | ||
tm.assert_frame_equal(df, sorted_df) | ||
|
||
def test_sort_values_stable_descending_multicolumn_sort(self): | ||
@pytest.mark.parametrize( | ||
"expected_idx_non_na, ascending", | ||
[ | ||
[ | ||
[3, 4, 5, 0, 1, 8, 6, 9, 7, 10, 13, 14], | ||
[True, True], | ||
], | ||
[ | ||
[0, 3, 4, 5, 1, 8, 6, 7, 10, 13, 14, 9], | ||
[True, False], | ||
], | ||
[ | ||
[9, 7, 10, 13, 14, 6, 8, 1, 3, 4, 5, 0], | ||
[False, True], | ||
], | ||
[ | ||
[7, 10, 13, 14, 9, 6, 8, 1, 0, 3, 4, 5], | ||
[False, False], | ||
], | ||
], | ||
) | ||
@pytest.mark.parametrize("na_position", ["first", "last"]) | ||
def test_sort_values_stable_multicolumn_sort( | ||
self, expected_idx_non_na, ascending, na_position | ||
): | ||
# GH#38426 Clarify sort_values with mult. columns / labels is stable | ||
df = DataFrame( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a comment with the GH id of this pr or the issue here and i'd say this is done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. I've added a reference to this PR. |
||
{"A": [1, 2, np.nan, 1, 6, 8, 4], "B": [9, np.nan, 5, 2, 5, 4, 5]} | ||
) | ||
# test stable mergesort | ||
expected = DataFrame( | ||
{"A": [np.nan, 8, 6, 4, 2, 1, 1], "B": [5, 4, 5, 5, np.nan, 2, 9]}, | ||
index=[2, 5, 4, 6, 1, 3, 0], | ||
) | ||
sorted_df = df.sort_values( | ||
["A", "B"], ascending=[0, 1], na_position="first", kind="mergesort" | ||
{ | ||
"A": [1, 2, np.nan, 1, 1, 1, 6, 8, 4, 8, 8, np.nan, np.nan, 8, 8], | ||
"B": [9, np.nan, 5, 2, 2, 2, 5, 4, 5, 3, 4, np.nan, np.nan, 4, 4], | ||
} | ||
) | ||
tm.assert_frame_equal(sorted_df, expected) | ||
|
||
expected = DataFrame( | ||
{"A": [np.nan, 8, 6, 4, 2, 1, 1], "B": [5, 4, 5, 5, np.nan, 9, 2]}, | ||
index=[2, 5, 4, 6, 1, 0, 3], | ||
# All rows with NaN in col "B" only have unique values in "A", therefore, | ||
# only the rows with NaNs in "A" have to be treated individually: | ||
expected_idx = ( | ||
[11, 12, 2] + expected_idx_non_na | ||
if na_position == "first" | ||
else expected_idx_non_na + [2, 11, 12] | ||
) | ||
expected = df.take(expected_idx) | ||
sorted_df = df.sort_values( | ||
["A", "B"], ascending=[0, 0], na_position="first", kind="mergesort" | ||
["A", "B"], ascending=ascending, na_position=na_position | ||
) | ||
tm.assert_frame_equal(sorted_df, expected) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.