Skip to content

REF: stricter types for RangeIndex._simple_new #31084

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 1 commit into from
Jan 16, 2020
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
6 changes: 5 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
ABCMultiIndex,
ABCPandasArray,
ABCPeriodIndex,
ABCRangeIndex,
ABCSeries,
ABCTimedeltaIndex,
)
Expand Down Expand Up @@ -3105,7 +3106,10 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None):
if not isinstance(target, Index) and len(target) == 0:
attrs = self._get_attributes_dict()
attrs.pop("freq", None) # don't preserve freq
values = self._data[:0] # appropriately-dtyped empty array
if isinstance(self, ABCRangeIndex):
Copy link
Member

Choose a reason for hiding this comment

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

What does this need to change for?

Copy link
Member Author

Choose a reason for hiding this comment

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

self._data[:0] is an ndarray for RangeIndex, so we can't pass it to self._simple_new below

values = range(0)
else:
values = self._data[:0] # appropriately-dtyped empty array
target = self._simple_new(values, dtype=self.dtype, **attrs)
else:
target = ensure_index(target)
Expand Down
19 changes: 8 additions & 11 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
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 Index, _index_shared_docs, maybe_extract_name
from pandas.core.indexes.base import _index_shared_docs, maybe_extract_name
from pandas.core.indexes.numeric import Int64Index
from pandas.core.ops.common import unpack_zerodim_and_defer

from pandas.io.formats.printing import pprint_thing

_empty_range = range(0)


class RangeIndex(Int64Index):
"""
Expand Down Expand Up @@ -130,15 +132,10 @@ def from_range(cls, data, name=None, dtype=None):
return cls._simple_new(data, dtype=dtype, name=name)

@classmethod
def _simple_new(cls, values, name=None, dtype=None):
def _simple_new(cls, values: range, name=None, dtype=None) -> "RangeIndex":
result = object.__new__(cls)

# handle passed None, non-integers
if values is None:
# empty
values = range(0, 0, 1)
elif not isinstance(values, range):
return Index(values, dtype=dtype, name=name)
assert isinstance(values, range)

result._range = values
result.name = name
Expand Down Expand Up @@ -482,7 +479,7 @@ def intersection(self, other, sort=False):
return super().intersection(other, sort=sort)

if not len(self) or not len(other):
return self._simple_new(None)
return self._simple_new(_empty_range)

first = self._range[::-1] if self.step < 0 else self._range
second = other._range[::-1] if other.step < 0 else other._range
Expand All @@ -492,7 +489,7 @@ def intersection(self, other, sort=False):
int_low = max(first.start, second.start)
int_high = min(first.stop, second.stop)
if int_high <= int_low:
return self._simple_new(None)
return self._simple_new(_empty_range)

# Method hint: linear Diophantine equation
# solve intersection problem
Expand All @@ -502,7 +499,7 @@ def intersection(self, other, sort=False):

# check whether element sets intersect
if (first.start - second.start) % gcd:
return self._simple_new(None)
return self._simple_new(_empty_range)

# calculate parameters for the RangeIndex describing the
# intersection disregarding the lower bounds
Expand Down