From e7c89b68a9c28a52a78c428e64ca4ff4b37f3bc7 Mon Sep 17 00:00:00 2001 From: tp Date: Tue, 20 Apr 2021 23:28:45 +0100 Subject: [PATCH 1/4] CLN: Clean-up numeric index dtype validation --- pandas/core/indexes/numeric.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 28144af36d6ea..93b5e4a322f65 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -1,4 +1,5 @@ from typing import ( + Callable, Hashable, Optional, ) @@ -49,6 +50,7 @@ class NumericIndex(Index): """ _default_dtype: np.dtype + _dtype_validation_metadata: tuple[Callable[..., bool], str] _is_numeric_dtype = True _can_hold_strings = False @@ -97,14 +99,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}" @@ -264,6 +260,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 = { @@ -280,6 +277,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 @@ -311,6 +309,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: From e7c0102b860dafcd4edd1769ae33040fc2b5e381 Mon Sep 17 00:00:00 2001 From: tp Date: Tue, 20 Apr 2021 23:57:59 +0100 Subject: [PATCH 2/4] import annotations from __future__ --- pandas/core/indexes/numeric.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 93b5e4a322f65..5bd9e66c9552d 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from typing import ( Callable, Hashable, From 69742766e08daa1eb4e3b2a79310dc914fd8247b Mon Sep 17 00:00:00 2001 From: tp Date: Wed, 21 Apr 2021 01:14:45 +0100 Subject: [PATCH 3/4] add dtype_validation_metadata to RangeIndex --- pandas/core/indexes/range.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index 1e974063bd839..0a2c0820f20a3 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -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 From e58da38892424f26cf0b8a4d66e5d3e469ed26a9 Mon Sep 17 00:00:00 2001 From: tp Date: Wed, 21 Apr 2021 07:39:39 +0100 Subject: [PATCH 4/4] fix pyupgrade --- pandas/core/indexes/numeric.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 5bd9e66c9552d..2182b3647533e 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -3,7 +3,6 @@ from typing import ( Callable, Hashable, - Optional, ) import warnings @@ -57,7 +56,7 @@ class NumericIndex(Index): _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)