Skip to content

Commit 24e1a1e

Browse files
jbrockmendelJulianWgs
authored andcommitted
REF: nargsort incorrectly calling _values_for_argsort (pandas-dev#37266)
1 parent 98df632 commit 24e1a1e

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

doc/source/whatsnew/v0.24.1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. _whatsnew_0241:
22

33
What's new in 0.24.1 (February 3, 2019)
4-
--------------------------------------
4+
---------------------------------------
55

66
.. warning::
77

doc/source/whatsnew/v0.24.2.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. _whatsnew_0242:
22

33
What's new in 0.24.2 (March 12, 2019)
4-
------------------------------------
4+
-------------------------------------
55

66
.. warning::
77

pandas/core/arrays/base.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,12 @@ def _values_for_argsort(self) -> np.ndarray:
507507
return np.array(self)
508508

509509
def argsort(
510-
self, ascending: bool = True, kind: str = "quicksort", *args, **kwargs
510+
self,
511+
ascending: bool = True,
512+
kind: str = "quicksort",
513+
na_position: str = "last",
514+
*args,
515+
**kwargs,
511516
) -> np.ndarray:
512517
"""
513518
Return the indices that would sort this array.
@@ -538,7 +543,14 @@ def argsort(
538543
# 2. argsort : total control over sorting.
539544
ascending = nv.validate_argsort_with_ascending(ascending, args, kwargs)
540545

541-
result = nargsort(self, kind=kind, ascending=ascending, na_position="last")
546+
values = self._values_for_argsort()
547+
result = nargsort(
548+
values,
549+
kind=kind,
550+
ascending=ascending,
551+
na_position=na_position,
552+
mask=np.asarray(self.isna()),
553+
)
542554
return result
543555

544556
def argmin(self):

pandas/core/sorting.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ def nargsort(
327327
ascending: bool = True,
328328
na_position: str = "last",
329329
key: Optional[Callable] = None,
330+
mask: Optional[np.ndarray] = None,
330331
):
331332
"""
332333
Intended to be a drop-in replacement for np.argsort which handles NaNs.
@@ -341,19 +342,27 @@ def nargsort(
341342
ascending : bool, default True
342343
na_position : {'first', 'last'}, default 'last'
343344
key : Optional[Callable], default None
345+
mask : Optional[np.ndarray], default None
346+
Passed when called by ExtensionArray.argsort.
344347
"""
345348

346349
if key is not None:
347350
items = ensure_key_mapped(items, key)
348351
return nargsort(
349-
items, kind=kind, ascending=ascending, na_position=na_position, key=None
352+
items,
353+
kind=kind,
354+
ascending=ascending,
355+
na_position=na_position,
356+
key=None,
357+
mask=mask,
350358
)
351359

352360
items = extract_array(items)
353-
mask = np.asarray(isna(items))
361+
if mask is None:
362+
mask = np.asarray(isna(items))
354363

355364
if is_extension_array_dtype(items):
356-
items = items._values_for_argsort()
365+
return items.argsort(ascending=ascending, kind=kind, na_position=na_position)
357366
else:
358367
items = np.asanyarray(items)
359368

0 commit comments

Comments
 (0)