Skip to content

Commit c6ce654

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

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ Performance Improvements
503503
- Improved performance of :meth:`Series.searchsorted`. The speedup is especially large when the dtype is
504504
int8/int16/int32 and the searched key is within the integer bounds for the dtype (:issue:`22034`)
505505
- Improved performance of :meth:`pandas.core.groupby.GroupBy.quantile` (:issue:`20405`)
506-
- Improved performance of slicing and other selected operation on a :class:`RangeIndex` (:issue:`26565`, :issue:`26617`)
506+
- Improved performance of slicing and other selected operation on a :class:`RangeIndex` (:issue:`26565`, :issue:`26617`, :issue:`26722`)
507507
- Improved performance of :meth:`read_csv` by faster tokenizing and faster parsing of small float numbers (:issue:`25784`)
508508
- Improved performance of :meth:`read_csv` by faster parsing of N/A and boolean values (:issue:`25804`)
509509
- Improved performance of :attr:`IntervalIndex.is_monotonic`, :attr:`IntervalIndex.is_monotonic_increasing` and :attr:`IntervalIndex.is_monotonic_decreasing` by removing conversion to :class:`MultiIndex` (:issue:`24813`)

pandas/core/indexes/range.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import numpy as np
88

9-
from pandas._libs import index as libindex, lib
9+
from pandas._libs import index as libindex
1010
import pandas.compat as compat
1111
from pandas.compat.numpy import function as nv
1212
from pandas.util._decorators import Appender, cache_readonly
@@ -604,23 +604,22 @@ def __getitem__(self, key):
604604
"""
605605
Conserve RangeIndex type for scalar and slice keys.
606606
"""
607-
if is_scalar(key):
608-
if not lib.is_integer(key):
609-
raise IndexError("only integers, slices (`:`), "
610-
"ellipsis (`...`), numpy.newaxis (`None`) "
611-
"and integer or boolean "
612-
"arrays are valid indices")
607+
if isinstance(key, slice):
608+
new_range = self._range[key]
609+
return self._simple_new(new_range, name=self.name)
610+
elif is_integer(key):
613611
new_key = int(key)
614612
try:
615613
return self._range[new_key]
616614
except IndexError:
617615
raise IndexError("index {key} is out of bounds for axis 0 "
618616
"with size {size}".format(key=key,
619617
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-
618+
elif is_scalar(key):
619+
raise IndexError("only integers, slices (`:`), "
620+
"ellipsis (`...`), numpy.newaxis (`None`) "
621+
"and integer or boolean "
622+
"arrays are valid indices")
624623
# fall back to Int64Index
625624
return super().__getitem__(key)
626625

0 commit comments

Comments
 (0)