31
31
32
32
if TYPE_CHECKING :
33
33
from pandas import MultiIndex
34
- from pandas .core .arrays import ExtensionArray
35
34
from pandas .core .indexes .base import Index
36
35
37
36
_INT64_MAX = np .iinfo (np .int64 ).max
@@ -391,7 +390,7 @@ def nargsort(
391
390
return indexer
392
391
393
392
394
- def nargminmax (values : "ExtensionArray" , method : str ) -> int :
393
+ def nargminmax (values , method : str ):
395
394
"""
396
395
Implementation of np.argmin/argmax but for ExtensionArray and which
397
396
handles missing values.
@@ -406,20 +405,16 @@ def nargminmax(values: "ExtensionArray", method: str) -> int:
406
405
int
407
406
"""
408
407
assert method in {"argmax" , "argmin" }
408
+ func = np .argmax if method == "argmax" else np .argmin
409
409
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 ()
414
412
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 ]
421
416
422
- return sorters [ 0 ]
417
+ return non_nan_idx [ func ( non_nans ) ]
423
418
424
419
425
420
def _ensure_key_mapped_multiindex (
0 commit comments