Skip to content

Commit 37c9523

Browse files
authored
DEPR: remove NumericIndex.__new__ & ._should_fallback_to_positional (#51118)
* DEPR: move NumericIndex._should_fallback_to_positional to Index * fix pre-commit * remove NumericIndex.__new__
1 parent 24a9073 commit 37c9523

File tree

3 files changed

+5
-29
lines changed

3 files changed

+5
-29
lines changed

pandas/core/indexes/base.py

+2
Original file line numberDiff line numberDiff line change
@@ -5689,6 +5689,8 @@ def _should_fallback_to_positional(self) -> bool:
56895689
"""
56905690
Should an integer key be treated as positional?
56915691
"""
5692+
if isinstance(self.dtype, np.dtype) and self.dtype.kind in ["i", "u", "f"]:
5693+
return False
56925694
return not self._holds_integer()
56935695

56945696
_index_shared_docs[

pandas/core/indexes/numeric.py

+1-27
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,7 @@
11
from __future__ import annotations
22

3-
import numpy as np
4-
5-
from pandas._typing import Dtype
6-
from pandas.util._decorators import (
7-
cache_readonly,
8-
doc,
9-
)
10-
113
from pandas.core.indexes.base import Index
124

135

146
class NumericIndex(Index):
15-
def __new__(
16-
cls, data=None, dtype: Dtype | None = None, copy: bool = False, name=None
17-
) -> NumericIndex:
18-
# temporary scaffolding, will be removed soon.
19-
if isinstance(data, list) and len(data) == 0:
20-
data = np.array([], dtype=np.int64)
21-
elif isinstance(data, range):
22-
data = np.arange(data.start, data.stop, data.step, dtype=np.int64)
23-
return super().__new__(
24-
cls, data=data, dtype=dtype, copy=copy, name=name
25-
) # type: ignore[return-value]
26-
27-
# ----------------------------------------------------------------
28-
# Indexing Methods
29-
30-
@cache_readonly
31-
@doc(Index._should_fallback_to_positional)
32-
def _should_fallback_to_positional(self) -> bool:
33-
return False
7+
pass

pandas/tests/indexes/test_base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ def test_fancy(self, simple_index):
486486
@pytest.mark.parametrize("dtype", [np.int_, np.bool_])
487487
def test_empty_fancy(self, index, dtype):
488488
empty_arr = np.array([], dtype=dtype)
489-
empty_index = type(index)([])
489+
empty_index = type(index)([], dtype=index.dtype)
490490

491491
assert index[[]].identical(empty_index)
492492
assert index[empty_arr].identical(empty_index)
@@ -500,7 +500,7 @@ def test_empty_fancy_raises(self, index):
500500
# DatetimeIndex is excluded, because it overrides getitem and should
501501
# be tested separately.
502502
empty_farr = np.array([], dtype=np.float_)
503-
empty_index = type(index)([])
503+
empty_index = type(index)([], dtype=index.dtype)
504504

505505
assert index[[]].identical(empty_index)
506506
# np.ndarray only accepts ndarray of int & bool dtypes, so should Index

0 commit comments

Comments
 (0)