diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 4a467680053208..7eb1542ea26827 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -1614,6 +1614,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 7ca627c57706b0..edbacc0378c081 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4712,10 +4712,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, @@ -4723,7 +4722,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) @@ -4766,7 +4765,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 1930f412a76e23..cd313b09605267 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2831,14 +2831,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. @@ -3029,7 +3029,7 @@ def _try_kind_sort(arr): else: return result.__finalize__(self) - def sort_index( # type: ignore + def sort_index( self, axis=0, level=None,