Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEPR: move NumericIndex._engine_type and .inferred_type to Index #50940

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -2545,6 +2560,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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesnt change anything for our nullable dtypes, but in principle could be an API change for 3rd party EAs. worth caring about @mroeschke ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be a good API change for ArrowDtype, but not sure what else subsequently relies on the output of Index.inferred_type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change I proposed isn't strictly needed, I just added it as an optimization. I could also remove it here and try do add something similar to lib.infer_dtype instead. It's also a better location for this IMO and at least we can discuss if there's some down side to doing this seperately from the NumericIndex removal process.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to avoid the api change you can do:

if isinstance(self.dtype, np.dtype) and self.dtype.kind in "iufc":
    [...]
return lib.infer_dtype(...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I can do it like that.

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