Skip to content

Commit c5255da

Browse files
kawochenjreback
authored andcommitted
fixed equals, added test cases, shortcut from_range if PY3
1 parent 56cef1b commit c5255da

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

pandas/core/index.py

+24-16
Original file line numberDiff line numberDiff line change
@@ -3990,19 +3990,24 @@ def from_range(cls, data, name=None, dtype=None, **kwargs):
39903990
'{0}(...) must be called with object coercible to a '
39913991
'range, {1} was passed'.format(cls.__name__, repr(data)))
39923992

3993+
if compat.PY3:
3994+
step = data.step
3995+
stop = data.stop
3996+
start = data.start
3997+
else:
39933998
# seems we only have indexing ops to infer
39943999
# rather than direct accessors
3995-
if len(data) > 1:
3996-
step = data[1] - data[0]
3997-
stop = data[-1] + step
3998-
start = data[0]
3999-
elif len(data):
4000-
start = data[0]
4001-
stop = data[0] + 1
4002-
step = 1
4003-
else:
4004-
start = stop = 0
4005-
step = 1
4000+
if len(data) > 1:
4001+
step = data[1] - data[0]
4002+
stop = data[-1] + step
4003+
start = data[0]
4004+
elif len(data):
4005+
start = data[0]
4006+
stop = data[0] + 1
4007+
step = 1
4008+
else:
4009+
start = stop = 0
4010+
step = 1
40064011
return RangeIndex(start, stop, step, dtype=dtype, name=name, **kwargs)
40074012

40084013
@classmethod
@@ -4153,11 +4158,14 @@ def equals(self, other):
41534158
Determines if two Index objects contain the same elements.
41544159
"""
41554160
if isinstance(other, RangeIndex):
4156-
return (len(self) == len(other) == 0
4157-
or (self._start == other._start and
4158-
self._stop == other._stop and
4159-
self._step == other._step)
4160-
)
4161+
ls = len(self)
4162+
lo = len(other)
4163+
return (ls == lo == 0 or
4164+
ls == lo == 1 and
4165+
self._start == other._start or
4166+
ls == lo and
4167+
self._start == other._start and
4168+
self._step == other._step)
41614169

41624170
return super(RangeIndex, self).equals(other)
41634171

pandas/tests/test_index.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -3717,11 +3717,13 @@ def test_is_monotonic(self):
37173717

37183718
def test_equals(self):
37193719

3720-
if isinstance(self.index, RangeIndex):
3721-
raise nose.SkipTest("RangeIndex does not accept dtype=object")
3722-
same_values = Index(self.index, dtype=object)
3723-
self.assertTrue(self.index.equals(same_values))
3724-
self.assertTrue(same_values.equals(self.index))
3720+
equiv_pairs = [(RangeIndex(0, 9, 2), RangeIndex(0, 10, 2)),
3721+
(RangeIndex(0), RangeIndex(1, -1, 3)),
3722+
(RangeIndex(1, 2, 3), RangeIndex(1, 3, 4)),
3723+
(RangeIndex(0, -9, -2), RangeIndex(0, -10, -2))]
3724+
for left, right in equiv_pairs:
3725+
self.assertTrue(left.equals(right))
3726+
self.assertTrue(right.equals(left))
37253727

37263728
def test_logical_compat(self):
37273729
idx = self.create_index()

0 commit comments

Comments
 (0)