|
23 | 23 |
|
24 | 24 | from pandas._typing import ArrayLike
|
25 | 25 | from pandas.core import ops
|
| 26 | +from pandas.core.sorting import nargsort |
26 | 27 |
|
27 | 28 | _not_implemented_message = "{} does not implement {}."
|
28 | 29 |
|
@@ -361,27 +362,6 @@ def isna(self) -> ArrayLike:
|
361 | 362 | """
|
362 | 363 | raise AbstractMethodError(self)
|
363 | 364 |
|
364 |
| - def _values_for_argsort(self) -> Tuple[np.ndarray, np.ndarray]: |
365 |
| - """ |
366 |
| - Return values for sorting. |
367 |
| -
|
368 |
| - Returns |
369 |
| - ------- |
370 |
| - ndarray |
371 |
| - The transformed values should maintain the ordering between values |
372 |
| - within the array. |
373 |
| - ndarray |
374 |
| - The mask which indicates the NA values. |
375 |
| -
|
376 |
| - .. versionadded:: 0.25.0 |
377 |
| -
|
378 |
| - See Also |
379 |
| - -------- |
380 |
| - ExtensionArray.argsort |
381 |
| - """ |
382 |
| - # Note: this is used in `ExtensionArray.argsort`. |
383 |
| - return np.array(self), self.isna() |
384 |
| - |
385 | 365 | def argsort(self, ascending=True, kind='quicksort', *args, **kwargs):
|
386 | 366 | """
|
387 | 367 | Return the indices that would sort this array.
|
@@ -410,25 +390,14 @@ def argsort(self, ascending=True, kind='quicksort', *args, **kwargs):
|
410 | 390 | # 1. _values_for_argsort : construct the values passed to np.argsort
|
411 | 391 | # 2. argsort : total control over sorting.
|
412 | 392 | ascending = nv.validate_argsort_with_ascending(ascending, args, kwargs)
|
413 |
| - values, mask = self._values_for_argsort() |
414 | 393 |
|
415 |
| - if mask.any(): |
416 |
| - notmask = ~mask |
417 |
| - notnull = np.argsort(values[notmask], kind=kind, **kwargs) |
418 |
| - |
419 |
| - # permu maps the indices of the subarray |
420 |
| - # without nan to the indices of the original array. |
421 |
| - permu = np.arange(len(mask)) |
422 |
| - permu = permu[~mask] |
423 |
| - |
424 |
| - notnull = permu[notnull] |
425 |
| - allnan = np.arange(len(self))[mask] |
426 |
| - result = np.append(notnull, allnan) |
| 394 | + if hasattr(self, '_values_for_argsort'): |
| 395 | + values = self._values_for_argsort() |
427 | 396 | else:
|
428 |
| - result = np.argsort(values, kind=kind, **kwargs) |
429 |
| - |
430 |
| - if not ascending: |
431 |
| - result = result[::-1] |
| 397 | + values = self |
| 398 | + na_position = 'last' if ascending else 'first' |
| 399 | + result = nargsort(values, kind=kind, ascending=ascending, |
| 400 | + na_position=na_position) |
432 | 401 | return result
|
433 | 402 |
|
434 | 403 | def fillna(self, value=None, method=None, limit=None):
|
|
0 commit comments