Skip to content

Commit dda8c35

Browse files
Revert "REF: define nanargminmax without values_for_argsort (#37815)" (#38177)
This reverts commit 840c142.
1 parent af48930 commit dda8c35

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

pandas/core/sorting.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
if TYPE_CHECKING:
3333
from pandas import MultiIndex
34-
from pandas.core.arrays import ExtensionArray
3534
from pandas.core.indexes.base import Index
3635

3736
_INT64_MAX = np.iinfo(np.int64).max
@@ -391,7 +390,7 @@ def nargsort(
391390
return indexer
392391

393392

394-
def nargminmax(values: "ExtensionArray", method: str) -> int:
393+
def nargminmax(values, method: str):
395394
"""
396395
Implementation of np.argmin/argmax but for ExtensionArray and which
397396
handles missing values.
@@ -406,20 +405,16 @@ def nargminmax(values: "ExtensionArray", method: str) -> int:
406405
int
407406
"""
408407
assert method in {"argmax", "argmin"}
408+
func = np.argmax if method == "argmax" else np.argmin
409409

410-
mask = np.asarray(values.isna())
411-
if mask.all():
412-
# Use same exception message we would get from numpy
413-
raise ValueError(f"attempt to get {method} of an empty sequence")
410+
mask = np.asarray(isna(values))
411+
values = values._values_for_argsort()
414412

415-
if method == "argmax":
416-
# Use argsort with ascending=False so that if more than one entry
417-
# achieves the maximum, we take the first such occurence.
418-
sorters = values.argsort(ascending=False)
419-
else:
420-
sorters = values.argsort(ascending=True)
413+
idx = np.arange(len(values))
414+
non_nans = values[~mask]
415+
non_nan_idx = idx[~mask]
421416

422-
return sorters[0]
417+
return non_nan_idx[func(non_nans)]
423418

424419

425420
def _ensure_key_mapped_multiindex(

0 commit comments

Comments
 (0)