diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index daa824c301a82..144665401a2a0 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -4,7 +4,6 @@ import operator from sys import getsizeof from typing import ( - TYPE_CHECKING, Any, Callable, Hashable, @@ -46,13 +45,13 @@ import pandas.core.common as com from pandas.core.construction import extract_array import pandas.core.indexes.base as ibase -from pandas.core.indexes.base import maybe_extract_name +from pandas.core.indexes.base import ( + Index, + maybe_extract_name, +) from pandas.core.indexes.numeric import NumericIndex from pandas.core.ops.common import unpack_zerodim_and_defer -if TYPE_CHECKING: - from pandas import Index - _empty_range = range(0) @@ -192,10 +191,10 @@ def _validate_dtype(cls, dtype: Dtype | None) -> None: # -------------------------------------------------------------------- - # error: Return type "Type[NumericIndex]" of "_constructor" incompatible with return + # error: Return type "Type[Index]" of "_constructor" incompatible with return # type "Type[RangeIndex]" in supertype "Index" @cache_readonly - def _constructor(self) -> type[NumericIndex]: # type: ignore[override] + def _constructor(self) -> type[Index]: # type: ignore[override] """return the class to use for construction""" return NumericIndex @@ -338,7 +337,7 @@ def inferred_type(self) -> str: # -------------------------------------------------------------------- # Indexing Methods - @doc(NumericIndex.get_loc) + @doc(Index.get_loc) def get_loc(self, key): if is_integer(key) or (is_float(key) and key.is_integer()): new_key = int(key) @@ -379,21 +378,28 @@ def _get_indexer( locs[valid] = len(self) - 1 - locs[valid] return ensure_platform_int(locs) + @cache_readonly + def _should_fallback_to_positional(self) -> bool: + """ + Should an integer key be treated as positional? + """ + return False + # -------------------------------------------------------------------- def tolist(self) -> list[int]: return list(self._range) - @doc(NumericIndex.__iter__) + @doc(Index.__iter__) def __iter__(self) -> Iterator[int]: yield from self._range - @doc(NumericIndex._shallow_copy) + @doc(Index._shallow_copy) def _shallow_copy(self, values, name: Hashable = no_default): name = self.name if name is no_default else name if values.dtype.kind == "f": - return NumericIndex(values, name=name, dtype=np.float64) + return Index(values, name=name, dtype=np.float64) # GH 46675 & 43885: If values is equally spaced, return a # more memory-compact RangeIndex instead of Index with 64-bit dtype unique_diffs = unique_deltas(values) @@ -409,7 +415,7 @@ def _view(self: RangeIndex) -> RangeIndex: result._cache = self._cache return result - @doc(NumericIndex.copy) + @doc(Index.copy) def copy(self, name: Hashable = None, deep: bool = False): name = self._validate_names(name=name, deep=deep)[0] new_index = self._rename(name=name)