Skip to content

DEPR: remove NumericIndex.__new__ & ._should_fallback_to_positional #51118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5689,6 +5689,8 @@ def _should_fallback_to_positional(self) -> bool:
"""
Should an integer key be treated as positional?
"""
if isinstance(self.dtype, np.dtype) and self.dtype.kind in ["i", "u", "f"]:
return False
return not self._holds_integer()

_index_shared_docs[
Expand Down
28 changes: 1 addition & 27 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,7 @@
from __future__ import annotations

import numpy as np

from pandas._typing import Dtype
from pandas.util._decorators import (
cache_readonly,
doc,
)

from pandas.core.indexes.base import Index


class NumericIndex(Index):
def __new__(
cls, data=None, dtype: Dtype | None = None, copy: bool = False, name=None
) -> NumericIndex:
# temporary scaffolding, will be removed soon.
if isinstance(data, list) and len(data) == 0:
data = np.array([], dtype=np.int64)
elif isinstance(data, range):
data = np.arange(data.start, data.stop, data.step, dtype=np.int64)
return super().__new__(
cls, data=data, dtype=dtype, copy=copy, name=name
) # type: ignore[return-value]

# ----------------------------------------------------------------
# Indexing Methods

@cache_readonly
@doc(Index._should_fallback_to_positional)
def _should_fallback_to_positional(self) -> bool:
return False
pass
4 changes: 2 additions & 2 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ def test_fancy(self, simple_index):
@pytest.mark.parametrize("dtype", [np.int_, np.bool_])
def test_empty_fancy(self, index, dtype):
empty_arr = np.array([], dtype=dtype)
empty_index = type(index)([])
empty_index = type(index)([], dtype=index.dtype)

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

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