-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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…
jacobaustin123 e05462a
fixed a few small bugs
jacobaustin123 0f33c5c
bug fixes
jacobaustin123 b7d76cd
fixed
jacobaustin123 cf1fb5a
Merge branch 'master' of http://github.com/pandas-dev/pandas
jacobaustin123 8343f76
fixed
jacobaustin123 94281d3
fixed
jacobaustin123 c505dd9
updated docstrings
jacobaustin123 ecb6910
fixed documentation
jacobaustin123 55c444e
fixed
jacobaustin123 9d6762b
merged with master
jacobaustin123 d774b15
updated docs
jacobaustin123 64e70b4
linting
jacobaustin123 03d6573
fixed tests
jacobaustin123 9f5209e
merged
jacobaustin123 81c0172
reformatted
jacobaustin123 6d0d725
fixed linting issue
jacobaustin123 ef72542
fixed conflicts
jacobaustin123 0aabf56
fixed formatting
jacobaustin123 210df50
ENH: made sort_index apply the key to each level separately
jacobaustin123 b40a963
fixed a bug with duplicate names
jacobaustin123 90e2cfe
fixed strange bug with duplicate column names
jacobaustin123 8e12404
Merge branch 'master' of http://github.com/pandas-dev/pandas
jacobaustin123 447c48f
fixed bug
jacobaustin123 46171f0
fixed linting
jacobaustin123 a44a999
fixed linting issues
jacobaustin123 94b795c
disabled tests temporarily
jacobaustin123 6e651c0
fixed linting
jacobaustin123 5a92484
Merge branch 'master' of https://github.com/pandas-dev/pandas
jacobaustin123 fbdfc1e
reverted changes due to 33134
jacobaustin123 c56dbd6
updated documentation
jacobaustin123 77f44bf
fixed merge conflict
jacobaustin123 2106d86
Merge branch 'master' of https://github.com/pandas-dev/pandas
jacobaustin123 620f57a
updated docs
jacobaustin123 6a5bc32
fixed linting issue
jacobaustin123 5b244fb
try to recover from invalid type in output
jacobaustin123 6f15e66
fixed linting issue
jacobaustin123 7d2037b
added more tests
jacobaustin123 5048944
added some more tests
jacobaustin123 3b2d176
merged
jacobaustin123 0e239c8
fixed linting issue
jacobaustin123 bc44d0d
major documentation additions, removed key for Categorical
jacobaustin123 07d903c
doc linting issue
jacobaustin123 ecdbf4c
another linting fix
jacobaustin123 c376a74
fixed linting actually
jacobaustin123 f5e5808
moved apply_key to sorting.py
jacobaustin123 1058839
fixed tests
jacobaustin123 c87a527
satisfied mypy
jacobaustin123 e6026d6
fixed isort issues
jacobaustin123 ab0b887
fixed a doc issue
jacobaustin123 364cc5e
wow linting is hard
jacobaustin123 8db09d0
updated whatsnew
jacobaustin123 7477fd1
Merge branch 'master' of https://github.com/pandas-dev/pandas
jacobaustin123 1d0319c
cleaned up sorting.py
jacobaustin123 1f60689
fixed indentation
jacobaustin123 2957e60
removed trailing whitespace
jacobaustin123 7c6c2f0
linting
jacobaustin123 ad745c4
fixed small bug with datetimelike, updated docs
jacobaustin123 3ad3358
fixed trailing whitespace
jacobaustin123 e87a9a9
Merge branch 'master' of https://github.com/pandas-dev/pandas
jacobaustin123 a5d5c6d
reverted and updated documentation
jacobaustin123 56f73ba
merged and updated
jacobaustin123 4250e31
fixed linting issue and added comments
jacobaustin123 4d5ba53
fixed small issue in tests
jacobaustin123 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`). | ||
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.