Skip to content

REF: nargsort incorrectly calling _values_for_argsort #37266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.24.1.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.. _whatsnew_0241:

What's new in 0.24.1 (February 3, 2019)
--------------------------------------
---------------------------------------

.. warning::

Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.24.2.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.. _whatsnew_0242:

What's new in 0.24.2 (March 12, 2019)
------------------------------------
-------------------------------------

.. warning::

Expand Down
16 changes: 14 additions & 2 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,12 @@ def _values_for_argsort(self) -> np.ndarray:
return np.array(self)

def argsort(
self, ascending: bool = True, kind: str = "quicksort", *args, **kwargs
self,
ascending: bool = True,
kind: str = "quicksort",
na_position: str = "last",
*args,
**kwargs,
) -> np.ndarray:
"""
Return the indices that would sort this array.
Expand Down Expand Up @@ -538,7 +543,14 @@ def argsort(
# 2. argsort : total control over sorting.
ascending = nv.validate_argsort_with_ascending(ascending, args, kwargs)

result = nargsort(self, kind=kind, ascending=ascending, na_position="last")
values = self._values_for_argsort()
result = nargsort(
values,
kind=kind,
ascending=ascending,
na_position=na_position,
mask=np.asarray(self.isna()),
)
return result

def argmin(self):
Expand Down
15 changes: 12 additions & 3 deletions pandas/core/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ def nargsort(
ascending: bool = True,
na_position: str = "last",
key: Optional[Callable] = None,
mask: Optional[np.ndarray] = None,
):
"""
Intended to be a drop-in replacement for np.argsort which handles NaNs.
Expand All @@ -341,19 +342,27 @@ def nargsort(
ascending : bool, default True
na_position : {'first', 'last'}, default 'last'
key : Optional[Callable], default None
mask : Optional[np.ndarray], default None
Passed when called by ExtensionArray.argsort.
"""

if key is not None:
items = ensure_key_mapped(items, key)
return nargsort(
items, kind=kind, ascending=ascending, na_position=na_position, key=None
items,
kind=kind,
ascending=ascending,
na_position=na_position,
key=None,
mask=mask,
)

items = extract_array(items)
mask = np.asarray(isna(items))
if mask is None:
mask = np.asarray(isna(items))

if is_extension_array_dtype(items):
items = items._values_for_argsort()
return items.argsort(ascending=ascending, kind=kind, na_position=na_position)
else:
items = np.asanyarray(items)

Expand Down