Skip to content

Commit 5be3917

Browse files
committed
Revert "Remove _values_for_argsort"
This reverts commit 44b6d72.
1 parent 44b6d72 commit 5be3917

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

pandas/core/arrays/base.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,20 @@ def isna(self):
217217
"""
218218
raise AbstractMethodError(self)
219219

220+
def _values_for_argsort(self):
221+
# type: () -> ndarray
222+
"""Get the ndarray to be passed to np.argsort.
223+
224+
This is called from within 'ExtensionArray.argsort'.
225+
226+
Returns
227+
-------
228+
values : ndarray
229+
"""
230+
return np.array(self)
231+
220232
def argsort(self, ascending=True, kind='quicksort', *args, **kwargs):
221-
"""Return the indices that would sort this array.
233+
"""Returns the indices that would sort this array.
222234
223235
Parameters
224236
----------
@@ -239,8 +251,13 @@ def argsort(self, ascending=True, kind='quicksort', *args, **kwargs):
239251
--------
240252
numpy.argsort
241253
"""
254+
# Implementor note: You have two places to override the behavior of
255+
# argsort.
256+
# 1. _values_for_argsort : construct the values passed to np.argsort
257+
# 2. argsort : total control over sorting.
258+
242259
ascending = nv.validate_argsort_with_ascending(ascending, args, kwargs)
243-
values = self.astype(object)
260+
values = self._values_for_argsort()
244261
result = np.argsort(values, kind=kind, **kwargs)
245262
if not ascending:
246263
result = result[::-1]

pandas/core/arrays/categorical.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,9 @@ def check_for_ordered(self, op):
13861386
"you can use .as_ordered() to change the "
13871387
"Categorical to an ordered one\n".format(op=op))
13881388

1389+
def _values_for_argsort(self):
1390+
return self._codes.copy()
1391+
13891392
def argsort(self, ascending=True, kind='quicksort', *args, **kwargs):
13901393
"""
13911394
Returns the indices that would sort the Categorical instance if
@@ -1406,11 +1409,9 @@ def argsort(self, ascending=True, kind='quicksort', *args, **kwargs):
14061409
--------
14071410
numpy.ndarray.argsort
14081411
"""
1409-
ascending = nv.validate_argsort_with_ascending(ascending, args, kwargs)
1410-
result = np.argsort(self._codes.copy(), kind=kind, **kwargs)
1411-
if not ascending:
1412-
result = result[::-1]
1413-
return result
1412+
# Keep the implementation here just for the docstring.
1413+
return super(Categorical, self).argsort(ascending=ascending, kind=kind,
1414+
*args, **kwargs)
14141415

14151416
def sort_values(self, inplace=False, ascending=True, na_position='last'):
14161417
""" Sorts the Categorical by category value returning a new

0 commit comments

Comments
 (0)