diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index 38553bc1be8d6..c457b52cf4b0e 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -169,13 +169,17 @@ def _isna(obj, inf_as_na: bool = False): return False elif isinstance(obj, (np.ndarray, ABCExtensionArray)): return _isna_array(obj, inf_as_na=inf_as_na) - elif isinstance(obj, (ABCSeries, ABCIndex)): + elif isinstance(obj, ABCIndex): + # Try to use cached isna, which also short-circuits for integer dtypes + # and avoids materializing RangeIndex._values + if not obj._can_hold_na: + return obj.isna() + return _isna_array(obj._values, inf_as_na=inf_as_na) + + elif isinstance(obj, ABCSeries): result = _isna_array(obj._values, inf_as_na=inf_as_na) # box - if isinstance(obj, ABCSeries): - result = obj._constructor( - result, index=obj.index, name=obj.name, copy=False - ) + result = obj._constructor(result, index=obj.index, name=obj.name, copy=False) return result elif isinstance(obj, ABCDataFrame): return obj.isna() diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index 8aedc56d8e1cd..aed7a7a467db3 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -496,6 +496,7 @@ def argsort(self, *args, **kwargs) -> npt.NDArray[np.intp]: numpy.ndarray.argsort """ ascending = kwargs.pop("ascending", True) # EA compat + kwargs.pop("kind", None) # e.g. "mergesort" is irrelevant nv.validate_argsort(args, kwargs) if self._range.step > 0: diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 8f37413dd53c8..e34620d4caf17 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -333,11 +333,9 @@ def test_numpy_argsort(self, index): expected = index.argsort() tm.assert_numpy_array_equal(result, expected) - if not isinstance(index, RangeIndex): - # TODO: add compatibility to RangeIndex? - result = np.argsort(index, kind="mergesort") - expected = index.argsort(kind="mergesort") - tm.assert_numpy_array_equal(result, expected) + result = np.argsort(index, kind="mergesort") + expected = index.argsort(kind="mergesort") + tm.assert_numpy_array_equal(result, expected) # these are the only two types that perform # pandas compatibility input validation - the