Skip to content

Commit 6f3471a

Browse files
committed
better performance for slices
1 parent 4d46e5d commit 6f3471a

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

pandas/core/indexes/range.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,10 @@ def __getitem__(self, key):
604604
"""
605605
Conserve RangeIndex type for scalar and slice keys.
606606
"""
607-
if is_scalar(key):
607+
if isinstance(key, slice):
608+
new_range = self._range[key]
609+
return self._simple_new(new_range, name=self.name)
610+
elif is_scalar(key):
608611
if not lib.is_integer(key):
609612
raise IndexError("only integers, slices (`:`), "
610613
"ellipsis (`...`), numpy.newaxis (`None`) "
@@ -617,10 +620,6 @@ def __getitem__(self, key):
617620
raise IndexError("index {key} is out of bounds for axis 0 "
618621
"with size {size}".format(key=key,
619622
size=len(self)))
620-
if isinstance(key, slice):
621-
new_range = self._range[key]
622-
return self.from_range(new_range, name=self.name)
623-
624623
# fall back to Int64Index
625624
return super().__getitem__(key)
626625

0 commit comments

Comments
 (0)