Skip to content

Commit 1f52f8b

Browse files
committed
simplify _ensure_dtype
1 parent 4b8385c commit 1f52f8b

File tree

2 files changed

+7
-22
lines changed

2 files changed

+7
-22
lines changed

pandas/core/indexes/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,9 @@ def _outer_indexer(
359359
_is_numeric_dtype: bool = False
360360
_can_hold_na: bool = True
361361
_can_hold_strings: bool = True
362+
362363
# Whether this index is a NumericIndex, but not a Int64Index, Float64Index,
363-
# UInt64Index or RangeIndex
364+
# UInt64Index or RangeIndex. Needed for backwards compat. Remove in pandas 2.0.
364365
_is_numeric_index: bool = False
365366

366367
_engine_type: type[libindex.IndexEngine] = libindex.ObjectEngine

pandas/core/indexes/numeric.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,11 @@ def _ensure_dtype(cls, dtype: Dtype | None) -> np.dtype | None:
205205

206206
dtype = pandas_dtype(dtype)
207207
assert isinstance(dtype, np.dtype)
208-
return dtype
208+
209+
if cls._is_numeric_index: # NumericIndex
210+
return dtype
211+
else: # Int64Index, UInt64Index etc.
212+
return cls._default_dtype
209213

210214
def __contains__(self, key) -> bool:
211215
"""
@@ -354,16 +358,6 @@ class IntegerIndex(NumericIndex):
354358

355359
_is_numeric_index: bool = False
356360

357-
@classmethod
358-
@doc(NumericIndex._ensure_dtype)
359-
def _ensure_dtype(cls, dtype: Dtype | None) -> np.dtype | None:
360-
if dtype is None:
361-
return cls._default_dtype
362-
dtype = pandas_dtype(dtype)
363-
assert isinstance(dtype, np.dtype)
364-
365-
return cls._default_dtype
366-
367361
@property
368362
def asi8(self) -> np.ndarray:
369363
# do not cache or you'll create a memory leak
@@ -429,13 +423,3 @@ class Float64Index(NumericIndex):
429423
_default_dtype = np.dtype(np.float64)
430424
_dtype_validation_metadata = (is_float_dtype, "float")
431425
_is_numeric_index: bool = False
432-
433-
@classmethod
434-
@doc(NumericIndex._ensure_dtype)
435-
def _ensure_dtype(cls, dtype: Dtype | None) -> np.dtype | None:
436-
if dtype is None:
437-
return cls._default_dtype
438-
dtype = pandas_dtype(dtype)
439-
assert isinstance(dtype, np.dtype)
440-
441-
return cls._default_dtype

0 commit comments

Comments
 (0)