Skip to content

Commit 4707273

Browse files
committed
REF: Split argsort into two parts
1 parent 0ec3600 commit 4707273

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

pandas/core/arrays/base.py

+26-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import numpy as np
33

44
from pandas.errors import AbstractMethodError
5+
from pandas.compat.numpy import function as nv
56

67
_not_implemented_message = "{} does not implement {}."
78

@@ -216,11 +217,24 @@ def isna(self):
216217
"""
217218
raise AbstractMethodError(self)
218219

219-
def argsort(self, axis=-1, kind='quicksort', order=None):
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+
232+
def argsort(self, ascending=True, kind='quicksort', *args, **kwargs):
220233
"""Returns the indices that would sort this array.
221234
222235
Parameters
223236
----------
237+
ascending : bool, default True
224238
axis : int or None, optional
225239
Axis along which to sort. ExtensionArrays are 1-dimensional,
226240
so this is only included for compatibility with NumPy.
@@ -233,9 +247,18 @@ def argsort(self, axis=-1, kind='quicksort', order=None):
233247
-------
234248
index_array : ndarray
235249
Array of indices that sort ``self``.
236-
237250
"""
238-
return np.array(self).argsort(kind=kind)
251+
# Implementor note: You have two places to override the behavior of
252+
# argsort.
253+
# 1. _values_for_argsort : construct the values passed to np.argsort
254+
# 2. argsort : total control over sorting.
255+
256+
ascending = nv.validate_argsort_with_ascending(ascending, args, kwargs)
257+
values = self._values_for_argsort()
258+
result = np.argsort(values, kind=kind, **kwargs)
259+
if not ascending:
260+
result = result[::-1]
261+
return result
239262

240263
# ------------------------------------------------------------------------
241264
# Indexing methods

pandas/core/arrays/categorical.py

+6-5
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)