Skip to content

Commit e7c89b6

Browse files
committed
CLN: Clean-up numeric index dtype validation
1 parent 503ce50 commit e7c89b6

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

pandas/core/indexes/numeric.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import (
2+
Callable,
23
Hashable,
34
Optional,
45
)
@@ -49,6 +50,7 @@ class NumericIndex(Index):
4950
"""
5051

5152
_default_dtype: np.dtype
53+
_dtype_validation_metadata: tuple[Callable[..., bool], str]
5254

5355
_is_numeric_dtype = True
5456
_can_hold_strings = False
@@ -97,14 +99,8 @@ def _ensure_array(cls, data, dtype, copy: bool):
9799
def _validate_dtype(cls, dtype: Dtype) -> None:
98100
if dtype is None:
99101
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]
102+
103+
validation_func, expected = cls._dtype_validation_metadata
108104
if not validation_func(dtype):
109105
raise ValueError(
110106
f"Incorrect `dtype` passed: expected {expected}, received {dtype}"
@@ -264,6 +260,7 @@ class Int64Index(IntegerIndex):
264260
_typ = "int64index"
265261
_engine_type = libindex.Int64Engine
266262
_default_dtype = np.dtype(np.int64)
263+
_dtype_validation_metadata = (is_signed_integer_dtype, "signed integer")
267264

268265

269266
_uint64_descr_args = {
@@ -280,6 +277,7 @@ class UInt64Index(IntegerIndex):
280277
_typ = "uint64index"
281278
_engine_type = libindex.UInt64Engine
282279
_default_dtype = np.dtype(np.uint64)
280+
_dtype_validation_metadata = (is_unsigned_integer_dtype, "unsigned integer")
283281

284282
# ----------------------------------------------------------------
285283
# Indexing Methods
@@ -311,6 +309,7 @@ class Float64Index(NumericIndex):
311309
_typ = "float64index"
312310
_engine_type = libindex.Float64Engine
313311
_default_dtype = np.dtype(np.float64)
312+
_dtype_validation_metadata = (is_float_dtype, "float")
314313

315314
@property
316315
def inferred_type(self) -> str:

0 commit comments

Comments
 (0)