3131
3232if 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
425420def _ensure_key_mapped_multiindex (
0 commit comments