-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
ENH: Added key option to df/series.sort_values(key=...) and df/series.sort_index(key=...) sorting #27237
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
ENH: Added key option to df/series.sort_values(key=...) and df/series.sort_index(key=...) sorting #27237
Changes from all commits
Commits
Show all changes
64 commits
Select commit
Hold shift + click to select a range
eddd918
ENH: added df/series.sort_values(key=...) and df/series.sort_index(ke…
e05462a
fixed a few small bugs
0f33c5c
bug fixes
b7d76cd
fixed
cf1fb5a
Merge branch 'master' of http://github.com/pandas-dev/pandas
8343f76
fixed
94281d3
fixed
c505dd9
updated docstrings
ecb6910
fixed documentation
55c444e
fixed
9d6762b
merged with master
d774b15
updated docs
64e70b4
linting
03d6573
fixed tests
9f5209e
merged
81c0172
reformatted
6d0d725
fixed linting issue
ef72542
fixed conflicts
0aabf56
fixed formatting
210df50
ENH: made sort_index apply the key to each level separately
b40a963
fixed a bug with duplicate names
90e2cfe
fixed strange bug with duplicate column names
8e12404
Merge branch 'master' of http://github.com/pandas-dev/pandas
447c48f
fixed bug
46171f0
fixed linting
a44a999
fixed linting issues
94b795c
disabled tests temporarily
6e651c0
fixed linting
5a92484
Merge branch 'master' of https://github.com/pandas-dev/pandas
fbdfc1e
reverted changes due to 33134
c56dbd6
updated documentation
77f44bf
fixed merge conflict
2106d86
Merge branch 'master' of https://github.com/pandas-dev/pandas
620f57a
updated docs
6a5bc32
fixed linting issue
5b244fb
try to recover from invalid type in output
6f15e66
fixed linting issue
7d2037b
added more tests
5048944
added some more tests
3b2d176
merged
0e239c8
fixed linting issue
bc44d0d
major documentation additions, removed key for Categorical
07d903c
doc linting issue
ecdbf4c
another linting fix
c376a74
fixed linting actually
f5e5808
moved apply_key to sorting.py
1058839
fixed tests
c87a527
satisfied mypy
e6026d6
fixed isort issues
ab0b887
fixed a doc issue
364cc5e
wow linting is hard
8db09d0
updated whatsnew
7477fd1
Merge branch 'master' of https://github.com/pandas-dev/pandas
1d0319c
cleaned up sorting.py
1f60689
fixed indentation
2957e60
removed trailing whitespace
7c6c2f0
linting
ad745c4
fixed small bug with datetimelike, updated docs
3ad3358
fixed trailing whitespace
e87a9a9
Merge branch 'master' of https://github.com/pandas-dev/pandas
a5d5c6d
reverted and updated documentation
56f73ba
merged and updated
4250e31
fixed linting issue and added comments
4d5ba53
fixed small issue in tests
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 |
|---|---|---|
|
|
@@ -36,6 +36,53 @@ For example: | |
| ser["2014"] | ||
| ser.loc["May 2015"] | ||
|
|
||
| .. _whatsnew_110.key_sorting: | ||
|
|
||
| Sorting with keys | ||
| ^^^^^^^^^^^^^^^^^ | ||
|
|
||
| We've added a ``key`` argument to the DataFrame and Series sorting methods, including | ||
| :meth:`DataFrame.sort_values`, :meth:`DataFrame.sort_index`, :meth:`Series.sort_values`, | ||
| and :meth:`Series.sort_index`. The ``key`` can be any callable function which is applied | ||
| column-by-column to each column used for sorting, before sorting is performed (:issue:`27237`). | ||
|
Contributor
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. can you also add the orginal issue number here (or rather replace this issue number ) |
||
| See :ref:`sort_values with keys <basics.sort_value_key>` and :ref:`sort_index with keys | ||
| <basics.sort_index_key>` for more information. | ||
|
|
||
| .. ipython:: python | ||
|
|
||
| s = pd.Series(['C', 'a', 'B']) | ||
| s | ||
|
|
||
| .. ipython:: python | ||
|
|
||
| s.sort_values() | ||
jacobaustin123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| Note how this is sorted with capital letters first. If we apply the :meth:`Series.str.lower` | ||
| method, we get | ||
|
|
||
| .. ipython:: python | ||
|
|
||
| s.sort_values(key=lambda x: x.str.lower()) | ||
|
|
||
|
|
||
| When applied to a `DataFrame`, they key is applied per-column to all columns or a subset if | ||
| `by` is specified, e.g. | ||
|
|
||
| .. ipython:: python | ||
|
|
||
| df = pd.DataFrame({'a': ['C', 'C', 'a', 'a', 'B', 'B'], | ||
| 'b': [1, 2, 3, 4, 5, 6]}) | ||
| df | ||
|
|
||
| .. ipython:: python | ||
|
|
||
| df.sort_values(by=['a'], key=lambda col: col.str.lower()) | ||
jacobaustin123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| For more details, see examples and documentation in :meth:`DataFrame.sort_values`, | ||
| :meth:`Series.sort_values`, and :meth:`~DataFrame.sort_index`. | ||
|
|
||
| .. _whatsnew_110.timestamp_fold_support: | ||
|
|
||
| Fold argument support in Timestamp constructor | ||
|
|
||
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
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
Oops, something went wrong.
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.