Skip to content

Commit 893efb8

Browse files
committed
Fix RangeIndex implementation to avoid test changes
1 parent d1620f6 commit 893efb8

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

pandas/core/indexes/range.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import numpy as np
77

88
from pandas._libs import index as libindex
9+
from pandas._libs import lib
910
import pandas.compat as compat
1011
from pandas.compat import get_range_parameters, lrange, range
1112
from pandas.compat.numpy import function as nv
@@ -502,7 +503,12 @@ def __getitem__(self, key):
502503
super_getitem = super(RangeIndex, self).__getitem__
503504

504505
if is_scalar(key):
505-
n = int(key)
506+
if not lib.is_integer(key):
507+
raise IndexError("only integers, slices (`:`), "
508+
"ellipsis (`...`), numpy.newaxis (`None`) "
509+
"and integer or boolean "
510+
"arrays are valid indices")
511+
n = com.cast_scalar_indexer(key)
506512
if n != key:
507513
return super_getitem(key)
508514
if n < 0:
@@ -653,7 +659,8 @@ def _evaluate_numeric_binop(self, other):
653659
return op(self._int64index, other)
654660
# TODO: Do attrs get handled reliably?
655661

656-
return _evaluate_numeric_binop
662+
name = '__{name}__'.format(name=op.__name__)
663+
return compat.set_function_name(_evaluate_numeric_binop, name, cls)
657664

658665
cls.__add__ = _make_evaluate_binop(operator.add)
659666
cls.__radd__ = _make_evaluate_binop(ops.radd)

pandas/tests/indexes/test_base.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -715,10 +715,7 @@ def test_empty_fancy_raises(self, attr):
715715
# FutureWarning from non-tuple sequence of nd indexing
716716
@pytest.mark.filterwarnings("ignore::FutureWarning")
717717
def test_getitem_error(self, indices, itm):
718-
error_type = IndexError
719-
if isinstance(indices, RangeIndex) and (itm == 'no_int'):
720-
error_type = ValueError
721-
with pytest.raises(error_type):
718+
with pytest.raises(IndexError):
722719
indices[itm]
723720

724721
def test_intersection(self):
@@ -2581,10 +2578,6 @@ def test_generated_op_names(opname, indices):
25812578
# pd.Index.__rsub__ does not exist; though the method does exist
25822579
# for subclasses. see GH#19723
25832580
return
2584-
if (isinstance(index, RangeIndex) and
2585-
opname in ['add', 'radd', 'sub', 'rsub',
2586-
'mul', 'rmul', 'truediv', 'rtruediv']):
2587-
pytest.skip("RangeIndex does operators differently")
25882581
opname = '__{name}__'.format(name=opname)
25892582
method = getattr(index, opname)
25902583
assert method.__name__ == opname

0 commit comments

Comments
 (0)