Skip to content

Commit 7ddee71

Browse files
committed
chabge _is_numeric_index to be an attribute
1 parent ec003ed commit 7ddee71

File tree

7 files changed

+15
-7
lines changed

7 files changed

+15
-7
lines changed

pandas/core/indexes/base.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ def _outer_indexer(
359359
_is_numeric_dtype: bool = False
360360
_can_hold_na: bool = True
361361
_can_hold_strings: bool = True
362+
# Whether this index is a NumericIndex, but not a Int64Index, Float64Index,
363+
# UInt64Index or RangeIndex
364+
_is_numeric_index: bool = False
362365

363366
_engine_type: type[libindex.IndexEngine] = libindex.ObjectEngine
364367
# whether we support partial string indexing. Overridden
@@ -437,7 +440,7 @@ def __new__(
437440
return Index._simple_new(data, name=name)
438441

439442
# index-like
440-
elif isinstance(data, Index) and data._is_numeric_index() and dtype is None:
443+
elif isinstance(data, Index) and data._is_numeric_index and dtype is None:
441444
return type(data)(data, name=name, copy=copy)
442445
elif isinstance(data, (np.ndarray, Index, ABCSeries)):
443446

@@ -5745,7 +5748,7 @@ def map(self, mapper, na_action=None):
57455748
# empty
57465749
attributes["dtype"] = self.dtype
57475750

5748-
if self._is_numeric_index() and is_numeric_dtype(new_values.dtype):
5751+
if self._is_numeric_index and is_numeric_dtype(new_values.dtype):
57495752
return type(self)(new_values, **attributes)
57505753

57515754
return Index(new_values, **attributes)

pandas/core/indexes/category.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def astype(self, dtype, copy: bool = True) -> Index:
291291
# the super method always returns Int64Index, UInt64Index and Float64Index
292292
# but if e.g. the categories are a NumIndex with dtype float32, we want to
293293
# return an index with the same dtype as self.categories.
294-
if categories._is_numeric_index():
294+
if categories._is_numeric_index:
295295
assert isinstance(categories, NumericIndex) # mypy complaint fix
296296
try:
297297
categories._validate_dtype(dtype)

pandas/core/indexes/numeric.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class NumericIndex(Index):
9797
)
9898
_is_numeric_dtype = True
9999
_can_hold_strings = False
100+
_is_numeric_index: bool = True
100101

101102
@cache_readonly
102103
def _can_hold_na(self) -> bool:
@@ -374,6 +375,8 @@ class IntegerIndex(NumericIndex):
374375
This is an abstract class for Int64Index, UInt64Index.
375376
"""
376377

378+
_is_numeric_index: bool = False
379+
377380
@property
378381
def asi8(self) -> np.ndarray:
379382
# do not cache or you'll create a memory leak
@@ -438,3 +441,4 @@ class Float64Index(NumericIndex):
438441
_engine_type = libindex.Float64Engine
439442
_default_dtype = np.dtype(np.float64)
440443
_dtype_validation_metadata = (is_float_dtype, "float")
444+
_is_numeric_index: bool = False

pandas/core/indexes/range.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class RangeIndex(NumericIndex):
101101
_engine_type = libindex.Int64Engine
102102
_dtype_validation_metadata = (is_signed_integer_dtype, "signed integer")
103103
_range: range
104+
_is_numeric_index: bool = False
104105

105106
# --------------------------------------------------------------------
106107
# Constructors

pandas/tests/base/test_unique.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_unique(index_or_series_obj):
2525
expected = pd.MultiIndex.from_tuples(unique_values)
2626
expected.names = obj.names
2727
tm.assert_index_equal(result, expected, exact=True)
28-
elif isinstance(obj, pd.Index) and obj._is_numeric_index():
28+
elif isinstance(obj, pd.Index) and obj._is_numeric_index:
2929
expected = NumericIndex(unique_values, dtype=obj.dtype)
3030
tm.assert_index_equal(result, expected, exact=True)
3131
elif isinstance(obj, pd.Index):
@@ -66,7 +66,7 @@ def test_unique_null(null_obj, index_or_series_obj):
6666
unique_values_not_null = [val for val in unique_values_raw if not pd.isnull(val)]
6767
unique_values = [null_obj] + unique_values_not_null
6868

69-
if isinstance(obj, pd.Index) and obj._is_numeric_index():
69+
if isinstance(obj, pd.Index) and obj._is_numeric_index:
7070
expected = NumericIndex(unique_values, dtype=obj.dtype)
7171
tm.assert_index_equal(result, expected, exact=True)
7272
elif isinstance(obj, pd.Index):

pandas/tests/indexes/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ def test_map_dictlike(self, mapper, simple_index):
665665
tm.assert_index_equal(result, expected)
666666

667667
# empty mappable
668-
if idx._is_numeric_index():
668+
if idx._is_numeric_index:
669669
new_index_cls = NumericIndex
670670
else:
671671
new_index_cls = Float64Index

pandas/tests/indexes/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ def test_map_dictlike(self, index, mapper):
713713
if index.empty:
714714
# to match proper result coercion for uints
715715
expected = Index([])
716-
elif index._is_numeric_index():
716+
elif index._is_numeric_index:
717717
expected = type(index)(np.arange(len(index), 0, -1), dtype=index.dtype)
718718
else:
719719
expected = Index(np.arange(len(index), 0, -1))

0 commit comments

Comments
 (0)