Skip to content

Commit ff4d9f9

Browse files
jbrockmendelKevin D Smith
authored and
Kevin D Smith
committed
REF: de-duplicate sort_values (pandas-dev#36301)
1 parent 60f398d commit ff4d9f9

File tree

2 files changed

+5
-20
lines changed

2 files changed

+5
-20
lines changed

pandas/core/indexes/base.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -4444,8 +4444,8 @@ def asof_locs(self, where, mask):
44444444

44454445
def sort_values(
44464446
self,
4447-
return_indexer=False,
4448-
ascending=True,
4447+
return_indexer: bool = False,
4448+
ascending: bool = True,
44494449
na_position: str_t = "last",
44504450
key: Optional[Callable] = None,
44514451
):
@@ -4509,7 +4509,9 @@ def sort_values(
45094509

45104510
# GH 35584. Sort missing values according to na_position kwarg
45114511
# ignore na_position for MutiIndex
4512-
if not isinstance(self, ABCMultiIndex):
4512+
if not isinstance(
4513+
self, (ABCMultiIndex, ABCDatetimeIndex, ABCTimedeltaIndex, ABCPeriodIndex)
4514+
):
45134515
_as = nargsort(
45144516
items=idx, ascending=ascending, na_position=na_position, key=key
45154517
)

pandas/core/indexes/datetimelike.py

-17
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
)
4242
from pandas.core.indexes.numeric import Int64Index
4343
from pandas.core.ops import get_op_result_name
44-
from pandas.core.sorting import ensure_key_mapped
4544
from pandas.core.tools.timedeltas import to_timedelta
4645

4746
_index_doc_kwargs = dict(ibase._index_doc_kwargs)
@@ -164,22 +163,6 @@ def __contains__(self, key: Any) -> bool:
164163
is_scalar(res) or isinstance(res, slice) or (is_list_like(res) and len(res))
165164
)
166165

167-
def sort_values(self, return_indexer=False, ascending=True, key=None):
168-
"""
169-
Return sorted copy of Index.
170-
"""
171-
idx = ensure_key_mapped(self, key)
172-
173-
_as = idx.argsort()
174-
if not ascending:
175-
_as = _as[::-1]
176-
sorted_index = self.take(_as)
177-
178-
if return_indexer:
179-
return sorted_index, _as
180-
else:
181-
return sorted_index
182-
183166
@Appender(_index_shared_docs["take"] % _index_doc_kwargs)
184167
def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
185168
nv.validate_take(tuple(), kwargs)

0 commit comments

Comments
 (0)