Skip to content

Commit d50c3cc

Browse files
authored
DEPR: move NumericIndex._engine_type and .inferred_type to Index (#50940)
* DEPR: move NumericIndex._engine_type and NumericIndex.inferred_type to Index * fix * update fastpath
1 parent 33f4f7b commit d50c3cc

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

pandas/core/indexes/base.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,26 @@ def _outer_indexer(
386386
_attributes: list[str] = ["name"]
387387
_can_hold_strings: bool = True
388388

389+
_engine_types: dict[np.dtype | ExtensionDtype, type[libindex.IndexEngine]] = {
390+
np.dtype(np.int8): libindex.Int8Engine,
391+
np.dtype(np.int16): libindex.Int16Engine,
392+
np.dtype(np.int32): libindex.Int32Engine,
393+
np.dtype(np.int64): libindex.Int64Engine,
394+
np.dtype(np.uint8): libindex.UInt8Engine,
395+
np.dtype(np.uint16): libindex.UInt16Engine,
396+
np.dtype(np.uint32): libindex.UInt32Engine,
397+
np.dtype(np.uint64): libindex.UInt64Engine,
398+
np.dtype(np.float32): libindex.Float32Engine,
399+
np.dtype(np.float64): libindex.Float64Engine,
400+
np.dtype(np.complex64): libindex.Complex64Engine,
401+
np.dtype(np.complex128): libindex.Complex128Engine,
402+
}
403+
389404
@property
390405
def _engine_type(
391406
self,
392407
) -> type[libindex.IndexEngine] | type[libindex.ExtensionEngine]:
393-
return libindex.ObjectEngine
408+
return self._engine_types.get(self.dtype, libindex.ObjectEngine)
394409

395410
# whether we support partial string indexing. Overridden
396411
# in DatetimeIndex and PeriodIndex
@@ -2554,6 +2569,13 @@ def inferred_type(self) -> str_t:
25542569
"""
25552570
Return a string of the type inferred from the values.
25562571
"""
2572+
if isinstance(self.dtype, np.dtype) and self.dtype.kind in "iufc": # fastpath
2573+
return {
2574+
"i": "integer",
2575+
"u": "integer",
2576+
"f": "floating",
2577+
"c": "complex",
2578+
}[self.dtype.kind]
25572579
return lib.infer_dtype(self._values, skipna=False)
25582580

25592581
@cache_readonly

pandas/core/indexes/numeric.py

-31
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import numpy as np
66

7-
from pandas._libs import index as libindex
87
from pandas._typing import Dtype
98
from pandas.util._decorators import (
109
cache_readonly,
@@ -74,36 +73,6 @@ class NumericIndex(Index):
7473
)
7574
_can_hold_strings = False
7675

77-
_engine_types: dict[np.dtype, type[libindex.IndexEngine]] = {
78-
np.dtype(np.int8): libindex.Int8Engine,
79-
np.dtype(np.int16): libindex.Int16Engine,
80-
np.dtype(np.int32): libindex.Int32Engine,
81-
np.dtype(np.int64): libindex.Int64Engine,
82-
np.dtype(np.uint8): libindex.UInt8Engine,
83-
np.dtype(np.uint16): libindex.UInt16Engine,
84-
np.dtype(np.uint32): libindex.UInt32Engine,
85-
np.dtype(np.uint64): libindex.UInt64Engine,
86-
np.dtype(np.float32): libindex.Float32Engine,
87-
np.dtype(np.float64): libindex.Float64Engine,
88-
np.dtype(np.complex64): libindex.Complex64Engine,
89-
np.dtype(np.complex128): libindex.Complex128Engine,
90-
}
91-
92-
@property
93-
def _engine_type(self) -> type[libindex.IndexEngine]:
94-
# error: Invalid index type "Union[dtype[Any], ExtensionDtype]" for
95-
# "Dict[dtype[Any], Type[IndexEngine]]"; expected type "dtype[Any]"
96-
return self._engine_types[self.dtype] # type: ignore[index]
97-
98-
@cache_readonly
99-
def inferred_type(self) -> str:
100-
return {
101-
"i": "integer",
102-
"u": "integer",
103-
"f": "floating",
104-
"c": "complex",
105-
}[self.dtype.kind]
106-
10776
def __new__(
10877
cls, data=None, dtype: Dtype | None = None, copy: bool = False, name=None
10978
) -> NumericIndex:

0 commit comments

Comments
 (0)