Skip to content

ENH: RangeIndex._shallow_copy can return RangeIndex #47557

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 11 commits into from
Jul 8, 2022
4 changes: 4 additions & 0 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,10 @@ def _shallow_copy(self, values, name: Hashable = no_default):

if values.dtype.kind == "f":
return Float64Index(values, name=name)
unique_diffs = np.unique(np.diff(values))
if len(unique_diffs) == 1:
new_range = range(values[0], values[-1], unique_diffs[0])
return type(self)._simple_new(new_range, name=name)
return Int64Index._simple_new(values, name=name)

def _view(self: RangeIndex) -> RangeIndex:
Expand Down