Skip to content

DEPR: Remove NumericIndex from indexes/range.py #51095

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
Merged
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
30 changes: 18 additions & 12 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import operator
from sys import getsizeof
from typing import (
TYPE_CHECKING,
Any,
Callable,
Hashable,
Expand Down Expand Up @@ -46,13 +45,13 @@
import pandas.core.common as com
from pandas.core.construction import extract_array
import pandas.core.indexes.base as ibase
from pandas.core.indexes.base import maybe_extract_name
from pandas.core.indexes.base import (
Index,
maybe_extract_name,
)
from pandas.core.indexes.numeric import NumericIndex
from pandas.core.ops.common import unpack_zerodim_and_defer

if TYPE_CHECKING:
from pandas import Index

_empty_range = range(0)


Expand Down Expand Up @@ -192,10 +191,10 @@ def _validate_dtype(cls, dtype: Dtype | None) -> None:

# --------------------------------------------------------------------

# error: Return type "Type[NumericIndex]" of "_constructor" incompatible with return
# error: Return type "Type[Index]" of "_constructor" incompatible with return
# type "Type[RangeIndex]" in supertype "Index"
@cache_readonly
def _constructor(self) -> type[NumericIndex]: # type: ignore[override]
def _constructor(self) -> type[Index]: # type: ignore[override]
"""return the class to use for construction"""
return NumericIndex
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is going to be updated to return Index before long?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, couldn't do it here because there is some issue with empty indexes (Index([]) and NumericIndex([]) are not the same).


Expand Down Expand Up @@ -338,7 +337,7 @@ def inferred_type(self) -> str:
# --------------------------------------------------------------------
# Indexing Methods

@doc(NumericIndex.get_loc)
@doc(Index.get_loc)
def get_loc(self, key):
if is_integer(key) or (is_float(key) and key.is_integer()):
new_key = int(key)
Expand Down Expand Up @@ -379,21 +378,28 @@ def _get_indexer(
locs[valid] = len(self) - 1 - locs[valid]
return ensure_platform_int(locs)

@cache_readonly
def _should_fallback_to_positional(self) -> bool:
"""
Should an integer key be treated as positional?
"""
return False

# --------------------------------------------------------------------

def tolist(self) -> list[int]:
return list(self._range)

@doc(NumericIndex.__iter__)
@doc(Index.__iter__)
def __iter__(self) -> Iterator[int]:
yield from self._range

@doc(NumericIndex._shallow_copy)
@doc(Index._shallow_copy)
def _shallow_copy(self, values, name: Hashable = no_default):
name = self.name if name is no_default else name

if values.dtype.kind == "f":
return NumericIndex(values, name=name, dtype=np.float64)
return Index(values, name=name, dtype=np.float64)
# GH 46675 & 43885: If values is equally spaced, return a
# more memory-compact RangeIndex instead of Index with 64-bit dtype
unique_diffs = unique_deltas(values)
Expand All @@ -409,7 +415,7 @@ def _view(self: RangeIndex) -> RangeIndex:
result._cache = self._cache
return result

@doc(NumericIndex.copy)
@doc(Index.copy)
def copy(self, name: Hashable = None, deep: bool = False):
name = self._validate_names(name=name, deep=deep)[0]
new_index = self._rename(name=name)
Expand Down