Skip to content

CLN: Clean-up numeric index dtype validation #41063

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

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

from typing import (
Callable,
Hashable,
Optional,
)
import warnings

Expand Down Expand Up @@ -49,11 +51,12 @@ class NumericIndex(Index):
"""

_default_dtype: np.dtype
_dtype_validation_metadata: tuple[Callable[..., bool], str]

_is_numeric_dtype = True
_can_hold_strings = False

def __new__(cls, data=None, dtype: Optional[Dtype] = None, copy=False, name=None):
def __new__(cls, data=None, dtype: Dtype | None = None, copy=False, name=None):
name = maybe_extract_name(name, data, cls)

subarr = cls._ensure_array(data, dtype, copy)
Expand Down Expand Up @@ -97,14 +100,8 @@ def _ensure_array(cls, data, dtype, copy: bool):
def _validate_dtype(cls, dtype: Dtype) -> None:
if dtype is None:
return
validation_metadata = {
"int64index": (is_signed_integer_dtype, "signed integer"),
"uint64index": (is_unsigned_integer_dtype, "unsigned integer"),
"float64index": (is_float_dtype, "float"),
"rangeindex": (is_signed_integer_dtype, "signed integer"),
}

validation_func, expected = validation_metadata[cls._typ]

validation_func, expected = cls._dtype_validation_metadata
if not validation_func(dtype):
raise ValueError(
f"Incorrect `dtype` passed: expected {expected}, received {dtype}"
Expand Down Expand Up @@ -264,6 +261,7 @@ class Int64Index(IntegerIndex):
_typ = "int64index"
_engine_type = libindex.Int64Engine
_default_dtype = np.dtype(np.int64)
_dtype_validation_metadata = (is_signed_integer_dtype, "signed integer")


_uint64_descr_args = {
Expand All @@ -280,6 +278,7 @@ class UInt64Index(IntegerIndex):
_typ = "uint64index"
_engine_type = libindex.UInt64Engine
_default_dtype = np.dtype(np.uint64)
_dtype_validation_metadata = (is_unsigned_integer_dtype, "unsigned integer")

# ----------------------------------------------------------------
# Indexing Methods
Expand Down Expand Up @@ -311,6 +310,7 @@ class Float64Index(NumericIndex):
_typ = "float64index"
_engine_type = libindex.Float64Engine
_default_dtype = np.dtype(np.float64)
_dtype_validation_metadata = (is_float_dtype, "float")

@property
def inferred_type(self) -> str:
Expand Down
1 change: 1 addition & 0 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class RangeIndex(NumericIndex):

_typ = "rangeindex"
_engine_type = libindex.Int64Engine
_dtype_validation_metadata = (is_signed_integer_dtype, "signed integer")
_can_hold_na = False
_range: range

Expand Down