-
-
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
ENH: Added key option to df/series.sort_values(key=...) and df/series.sort_index(key=...) sorting #27237
Changes from 34 commits
eddd918
e05462a
0f33c5c
b7d76cd
cf1fb5a
8343f76
94281d3
c505dd9
ecb6910
55c444e
9d6762b
d774b15
64e70b4
03d6573
9f5209e
81c0172
6d0d725
ef72542
0aabf56
210df50
b40a963
90e2cfe
8e12404
447c48f
46171f0
a44a999
94b795c
6e651c0
5a92484
fbdfc1e
c56dbd6
77f44bf
2106d86
620f57a
6a5bc32
5b244fb
6f15e66
7d2037b
5048944
3b2d176
0e239c8
bc44d0d
07d903c
ecdbf4c
c376a74
f5e5808
1058839
c87a527
e6026d6
ab0b887
364cc5e
8db09d0
7477fd1
1d0319c
1f60689
2957e60
7c6c2f0
ad745c4
3ad3358
e87a9a9
a5d5c6d
56f73ba
4250e31
4d5ba53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import operator | ||
from shutil import get_terminal_size | ||
from typing import Dict, Hashable, List, Type, Union, cast | ||
from typing import Callable, Dict, Hashable, List, Optional, Type, Union, cast | ||
from warnings import warn | ||
|
||
import numpy as np | ||
|
@@ -1532,7 +1532,13 @@ def argsort(self, ascending=True, kind="quicksort", **kwargs): | |
""" | ||
return super().argsort(ascending=ascending, kind=kind, **kwargs) | ||
|
||
def sort_values(self, inplace=False, ascending=True, na_position="last"): | ||
def sort_values( | ||
self, | ||
inplace=False, | ||
ascending=True, | ||
jacobaustin123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
na_position="last", | ||
key: Optional[Callable] = None, | ||
): | ||
""" | ||
Sort the Categorical by category value returning a new | ||
Categorical by default. | ||
|
@@ -1554,6 +1560,15 @@ def sort_values(self, inplace=False, ascending=True, na_position="last"): | |
na_position : {'first', 'last'} (optional, default='last') | ||
'first' puts NaNs at the beginning | ||
'last' puts NaNs at the end | ||
key : callable, optional | ||
Apply the key function to the values before sorting. | ||
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. you might want to give a sample of the key function here 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. Thinking more about this, I'm inclined to remove the key function here. I can't think of a use case for |
||
This is similar to the `key` argument in the builtin | ||
:meth:`sorted` function, with the notable difference that | ||
this `key` function should be *vectorized*. It should expect | ||
a ``Categorical`` and return an object with the same shape | ||
as the input. | ||
|
||
.. versionadded:: 1.1.0 | ||
|
||
Returns | ||
------- | ||
|
@@ -1610,7 +1625,9 @@ def sort_values(self, inplace=False, ascending=True, na_position="last"): | |
if na_position not in ["last", "first"]: | ||
raise ValueError(f"invalid na_position: {repr(na_position)}") | ||
|
||
sorted_idx = nargsort(self, ascending=ascending, na_position=na_position) | ||
sorted_idx = nargsort( | ||
self, ascending=ascending, na_position=na_position, key=key | ||
) | ||
|
||
if inplace: | ||
self._codes = self._codes[sorted_idx] | ||
|
Uh oh!
There was an error while loading. Please reload this page.