4
4
import operator
5
5
from sys import getsizeof
6
6
from typing import (
7
- TYPE_CHECKING ,
8
7
Any ,
9
8
Callable ,
10
9
Hashable ,
46
45
import pandas .core .common as com
47
46
from pandas .core .construction import extract_array
48
47
import pandas .core .indexes .base as ibase
49
- from pandas .core .indexes .base import maybe_extract_name
48
+ from pandas .core .indexes .base import (
49
+ Index ,
50
+ maybe_extract_name ,
51
+ )
50
52
from pandas .core .indexes .numeric import NumericIndex
51
53
from pandas .core .ops .common import unpack_zerodim_and_defer
52
54
53
- if TYPE_CHECKING :
54
- from pandas import Index
55
-
56
55
_empty_range = range (0 )
57
56
58
57
@@ -192,10 +191,10 @@ def _validate_dtype(cls, dtype: Dtype | None) -> None:
192
191
193
192
# --------------------------------------------------------------------
194
193
195
- # error: Return type "Type[NumericIndex ]" of "_constructor" incompatible with return
194
+ # error: Return type "Type[Index ]" of "_constructor" incompatible with return
196
195
# type "Type[RangeIndex]" in supertype "Index"
197
196
@cache_readonly
198
- def _constructor (self ) -> type [NumericIndex ]: # type: ignore[override]
197
+ def _constructor (self ) -> type [Index ]: # type: ignore[override]
199
198
"""return the class to use for construction"""
200
199
return NumericIndex
201
200
@@ -338,7 +337,7 @@ def inferred_type(self) -> str:
338
337
# --------------------------------------------------------------------
339
338
# Indexing Methods
340
339
341
- @doc (NumericIndex .get_loc )
340
+ @doc (Index .get_loc )
342
341
def get_loc (self , key ):
343
342
if is_integer (key ) or (is_float (key ) and key .is_integer ()):
344
343
new_key = int (key )
@@ -379,21 +378,28 @@ def _get_indexer(
379
378
locs [valid ] = len (self ) - 1 - locs [valid ]
380
379
return ensure_platform_int (locs )
381
380
381
+ @cache_readonly
382
+ def _should_fallback_to_positional (self ) -> bool :
383
+ """
384
+ Should an integer key be treated as positional?
385
+ """
386
+ return False
387
+
382
388
# --------------------------------------------------------------------
383
389
384
390
def tolist (self ) -> list [int ]:
385
391
return list (self ._range )
386
392
387
- @doc (NumericIndex .__iter__ )
393
+ @doc (Index .__iter__ )
388
394
def __iter__ (self ) -> Iterator [int ]:
389
395
yield from self ._range
390
396
391
- @doc (NumericIndex ._shallow_copy )
397
+ @doc (Index ._shallow_copy )
392
398
def _shallow_copy (self , values , name : Hashable = no_default ):
393
399
name = self .name if name is no_default else name
394
400
395
401
if values .dtype .kind == "f" :
396
- return NumericIndex (values , name = name , dtype = np .float64 )
402
+ return Index (values , name = name , dtype = np .float64 )
397
403
# GH 46675 & 43885: If values is equally spaced, return a
398
404
# more memory-compact RangeIndex instead of Index with 64-bit dtype
399
405
unique_diffs = unique_deltas (values )
@@ -409,7 +415,7 @@ def _view(self: RangeIndex) -> RangeIndex:
409
415
result ._cache = self ._cache
410
416
return result
411
417
412
- @doc (NumericIndex .copy )
418
+ @doc (Index .copy )
413
419
def copy (self , name : Hashable = None , deep : bool = False ):
414
420
name = self ._validate_names (name = name , deep = deep )[0 ]
415
421
new_index = self ._rename (name = name )
0 commit comments