2
2
import numpy as np
3
3
4
4
from pandas .errors import AbstractMethodError
5
+ from pandas .compat .numpy import function as nv
5
6
6
7
_not_implemented_message = "{} does not implement {}."
7
8
@@ -216,11 +217,24 @@ def isna(self):
216
217
"""
217
218
raise AbstractMethodError (self )
218
219
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 ):
220
233
"""Returns the indices that would sort this array.
221
234
222
235
Parameters
223
236
----------
237
+ ascending : bool, default True
224
238
axis : int or None, optional
225
239
Axis along which to sort. ExtensionArrays are 1-dimensional,
226
240
so this is only included for compatibility with NumPy.
@@ -233,9 +247,18 @@ def argsort(self, axis=-1, kind='quicksort', order=None):
233
247
-------
234
248
index_array : ndarray
235
249
Array of indices that sort ``self``.
236
-
237
250
"""
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
239
262
240
263
# ------------------------------------------------------------------------
241
264
# Indexing methods
0 commit comments