Skip to content

Commit

Permalink
DEPR: move NumericIndex._engine_type and .inferred_type to Index (#50940
Browse files Browse the repository at this point in the history
)

* DEPR: move NumericIndex._engine_type and NumericIndex.inferred_type to Index

* fix

* update fastpath
  • Loading branch information
topper-123 authored Jan 25, 2023
1 parent 33f4f7b commit d50c3cc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
24 changes: 23 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,26 @@ def _outer_indexer(
_attributes: list[str] = ["name"]
_can_hold_strings: bool = True

_engine_types: dict[np.dtype | ExtensionDtype, type[libindex.IndexEngine]] = {
np.dtype(np.int8): libindex.Int8Engine,
np.dtype(np.int16): libindex.Int16Engine,
np.dtype(np.int32): libindex.Int32Engine,
np.dtype(np.int64): libindex.Int64Engine,
np.dtype(np.uint8): libindex.UInt8Engine,
np.dtype(np.uint16): libindex.UInt16Engine,
np.dtype(np.uint32): libindex.UInt32Engine,
np.dtype(np.uint64): libindex.UInt64Engine,
np.dtype(np.float32): libindex.Float32Engine,
np.dtype(np.float64): libindex.Float64Engine,
np.dtype(np.complex64): libindex.Complex64Engine,
np.dtype(np.complex128): libindex.Complex128Engine,
}

@property
def _engine_type(
self,
) -> type[libindex.IndexEngine] | type[libindex.ExtensionEngine]:
return libindex.ObjectEngine
return self._engine_types.get(self.dtype, libindex.ObjectEngine)

# whether we support partial string indexing. Overridden
# in DatetimeIndex and PeriodIndex
Expand Down Expand Up @@ -2554,6 +2569,13 @@ def inferred_type(self) -> str_t:
"""
Return a string of the type inferred from the values.
"""
if isinstance(self.dtype, np.dtype) and self.dtype.kind in "iufc": # fastpath
return {
"i": "integer",
"u": "integer",
"f": "floating",
"c": "complex",
}[self.dtype.kind]
return lib.infer_dtype(self._values, skipna=False)

@cache_readonly
Expand Down
31 changes: 0 additions & 31 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import numpy as np

from pandas._libs import index as libindex
from pandas._typing import Dtype
from pandas.util._decorators import (
cache_readonly,
Expand Down Expand Up @@ -74,36 +73,6 @@ class NumericIndex(Index):
)
_can_hold_strings = False

_engine_types: dict[np.dtype, type[libindex.IndexEngine]] = {
np.dtype(np.int8): libindex.Int8Engine,
np.dtype(np.int16): libindex.Int16Engine,
np.dtype(np.int32): libindex.Int32Engine,
np.dtype(np.int64): libindex.Int64Engine,
np.dtype(np.uint8): libindex.UInt8Engine,
np.dtype(np.uint16): libindex.UInt16Engine,
np.dtype(np.uint32): libindex.UInt32Engine,
np.dtype(np.uint64): libindex.UInt64Engine,
np.dtype(np.float32): libindex.Float32Engine,
np.dtype(np.float64): libindex.Float64Engine,
np.dtype(np.complex64): libindex.Complex64Engine,
np.dtype(np.complex128): libindex.Complex128Engine,
}

@property
def _engine_type(self) -> type[libindex.IndexEngine]:
# error: Invalid index type "Union[dtype[Any], ExtensionDtype]" for
# "Dict[dtype[Any], Type[IndexEngine]]"; expected type "dtype[Any]"
return self._engine_types[self.dtype] # type: ignore[index]

@cache_readonly
def inferred_type(self) -> str:
return {
"i": "integer",
"u": "integer",
"f": "floating",
"c": "complex",
}[self.dtype.kind]

def __new__(
cls, data=None, dtype: Dtype | None = None, copy: bool = False, name=None
) -> NumericIndex:
Expand Down

0 comments on commit d50c3cc

Please sign in to comment.