Skip to content

Commit 09084bb

Browse files
author
Chang She
committed
ENH: allows Index - timedelta to make it symmetric to Index + timedelta
1 parent 4130ca7 commit 09084bb

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

pandas/core/index.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,10 @@ def __add__(self, other):
436436
__ge__ = _indexOp('__ge__')
437437

438438
def __sub__(self, other):
439-
return self.diff(other)
439+
if isinstance(other, Index):
440+
return self.diff(other)
441+
else:
442+
return Index(self.view(np.ndarray) - other)
440443

441444
def __and__(self, other):
442445
return self.intersection(other)

pandas/tests/test_index.py

+12
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,20 @@ def test_add(self):
221221
tm.assert_contains_all(self.strIndex, secondCat)
222222
tm.assert_contains_all(self.dateIndex, firstCat)
223223

224+
def test_timedelta(self):
224225
# this is valid too
225226
shifted = self.dateIndex + timedelta(1)
227+
back = shifted + timedelta(-1)
228+
self.assert_(tm.equalContents(self.dateIndex, back))
229+
self.assertEqual(shifted.freq, self.dateIndex.freq)
230+
self.assertEqual(shifted.freq, back.freq)
231+
232+
lead = self.dateIndex - timedelta(1)
233+
back = lead - timedelta(-1)
234+
self.assert_(tm.equalContents(self.dateIndex, back))
235+
self.assertEqual(self.dateIndex.freq, back.freq)
236+
self.assertEqual(lead.freq, back.freq)
237+
226238

227239
def test_append_multiple(self):
228240
index = Index(['a', 'b', 'c', 'd', 'e', 'f'])

0 commit comments

Comments
 (0)