|
6 | 6 |
|
7 | 7 | import numpy as np |
8 | 8 |
|
9 | | -from pandas._libs import index as libindex, lib |
| 9 | +from pandas._libs import index as libindex |
10 | 10 | import pandas.compat as compat |
11 | 11 | from pandas.compat.numpy import function as nv |
12 | 12 | from pandas.util._decorators import Appender, cache_readonly |
@@ -604,23 +604,22 @@ def __getitem__(self, key): |
604 | 604 | """ |
605 | 605 | Conserve RangeIndex type for scalar and slice keys. |
606 | 606 | """ |
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): |
613 | 611 | new_key = int(key) |
614 | 612 | try: |
615 | 613 | return self._range[new_key] |
616 | 614 | except IndexError: |
617 | 615 | raise IndexError("index {key} is out of bounds for axis 0 " |
618 | 616 | "with size {size}".format(key=key, |
619 | 617 | 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") |
624 | 623 | # fall back to Int64Index |
625 | 624 | return super().__getitem__(key) |
626 | 625 |
|
|
0 commit comments