diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 2675d198f64355..3c16999f37c552 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -1601,6 +1601,9 @@ def sort_values(self, inplace=False, ascending=True, na_position="last", key=Non na_position : {'first', 'last'} (optional, default='last') 'first' puts NaNs at the beginning 'last' puts NaNs at the end + key : Callable, default None + If not None, apply the key function to every value before + sorting. Identical to key argument in built-in sorted function. Returns ------- diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 9eb8d29b2924ed..05d803cdd696b5 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4705,10 +4705,9 @@ def f(vals): # ---------------------------------------------------------------------- # Sorting - @Substitution(**_shared_doc_kwargs) @Appender(NDFrame.sort_values.__doc__) - def sort_values( # type: ignore + def sort_values( self, by, axis=0, @@ -4716,7 +4715,7 @@ def sort_values( # type: ignore inplace=False, kind="quicksort", na_position="last", - key: Optional[Callable] = None, + key=None, ): inplace = validate_bool_kwarg(inplace, "inplace") axis = self._get_axis_number(axis) @@ -4759,7 +4758,7 @@ def sort_values( # type: ignore @Substitution(**_shared_doc_kwargs) @Appender(NDFrame.sort_index.__doc__) - def sort_index( # type: ignore + def sort_index( self, axis=0, level=None, diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 3c6673ee115e94..301d572131e0d4 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -279,6 +279,9 @@ def sort_values( """ Return sorted copy of Index. """ + if not isinstance(self, Index): + raise TypeError("sort_values must be called on an Index object") + if key: idx = self.map(key, na_action="ignore") else: @@ -308,7 +311,7 @@ def sort_values( if not ascending: sorted_values = sorted_values[::-1] - return self._simple_new(sorted_values, **attribs) # type: ignore + return self._simple_new(sorted_values, **attribs) @Appender(_index_shared_docs["take"] % _index_doc_kwargs) def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs): diff --git a/pandas/core/series.py b/pandas/core/series.py index fad3ed654da128..b7c35efdd8e0e6 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2828,14 +2828,14 @@ def update(self, other): # ---------------------------------------------------------------------- # Reindexing, sorting - def sort_values( # type: ignore + def sort_values( self, axis=0, ascending=True, inplace=False, kind="quicksort", na_position="last", - key: Optional[Callable] = None, + key=None, ): """ Sort by the values. @@ -3026,7 +3026,7 @@ def _try_kind_sort(arr): else: return result.__finalize__(self) - def sort_index( # type: ignore + def sort_index( self, axis=0, level=None,