Skip to content

Commit 32e1612

Browse files
topper-123yeshsurya
authored andcommitted
CLN: Clean-up numeric index dtype validation (pandas-dev#41063)
1 parent 8d1aa2f commit 32e1612

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

pandas/core/indexes/numeric.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
from __future__ import annotations
2+
13
from typing import (
4+
Callable,
25
Hashable,
3-
Optional,
46
)
57
import warnings
68

@@ -49,11 +51,12 @@ class NumericIndex(Index):
4951
"""
5052

5153
_default_dtype: np.dtype
54+
_dtype_validation_metadata: tuple[Callable[..., bool], str]
5255

5356
_is_numeric_dtype = True
5457
_can_hold_strings = False
5558

56-
def __new__(cls, data=None, dtype: Optional[Dtype] = None, copy=False, name=None):
59+
def __new__(cls, data=None, dtype: Dtype | None = None, copy=False, name=None):
5760
name = maybe_extract_name(name, data, cls)
5861

5962
subarr = cls._ensure_array(data, dtype, copy)
@@ -97,14 +100,8 @@ def _ensure_array(cls, data, dtype, copy: bool):
97100
def _validate_dtype(cls, dtype: Dtype) -> None:
98101
if dtype is None:
99102
return
100-
validation_metadata = {
101-
"int64index": (is_signed_integer_dtype, "signed integer"),
102-
"uint64index": (is_unsigned_integer_dtype, "unsigned integer"),
103-
"float64index": (is_float_dtype, "float"),
104-
"rangeindex": (is_signed_integer_dtype, "signed integer"),
105-
}
106-
107-
validation_func, expected = validation_metadata[cls._typ]
103+
104+
validation_func, expected = cls._dtype_validation_metadata
108105
if not validation_func(dtype):
109106
raise ValueError(
110107
f"Incorrect `dtype` passed: expected {expected}, received {dtype}"
@@ -264,6 +261,7 @@ class Int64Index(IntegerIndex):
264261
_typ = "int64index"
265262
_engine_type = libindex.Int64Engine
266263
_default_dtype = np.dtype(np.int64)
264+
_dtype_validation_metadata = (is_signed_integer_dtype, "signed integer")
267265

268266

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

284283
# ----------------------------------------------------------------
285284
# Indexing Methods
@@ -311,6 +310,7 @@ class Float64Index(NumericIndex):
311310
_typ = "float64index"
312311
_engine_type = libindex.Float64Engine
313312
_default_dtype = np.dtype(np.float64)
313+
_dtype_validation_metadata = (is_float_dtype, "float")
314314

315315
@property
316316
def inferred_type(self) -> str:

pandas/core/indexes/range.py

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class RangeIndex(NumericIndex):
9494

9595
_typ = "rangeindex"
9696
_engine_type = libindex.Int64Engine
97+
_dtype_validation_metadata = (is_signed_integer_dtype, "signed integer")
9798
_can_hold_na = False
9899
_range: range
99100

0 commit comments

Comments
 (0)