Skip to content

Commit 785537e

Browse files
author
Chang She
committed
BUG: keep frequency information when adding timedeltas
ENH: allow DatetimeIndex - timedelta
1 parent 09084bb commit 785537e

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

pandas/tseries/index.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,6 @@ class DatetimeIndex(Int64Index):
150150
__le__ = _dt_index_cmp('__le__')
151151
__ge__ = _dt_index_cmp('__ge__')
152152

153-
__add__ = _dt_index_op('__add__')
154-
__sub__ = _dt_index_op('__sub__')
155-
156153
# structured array cache for datetime fields
157154
_sarr_cache = None
158155

@@ -420,7 +417,16 @@ def __add__(self, other):
420417
return self.union(other)
421418
elif isinstance(other, (datetools.DateOffset, timedelta)):
422419
new_values = self.astype('O') + other
423-
return DatetimeIndex(new_values, tz=self.tz)
420+
return DatetimeIndex(new_values, tz=self.tz, freq=self.freq)
421+
else:
422+
return Index(self.view(np.ndarray) + other)
423+
424+
def __sub__(self, other):
425+
if isinstance(other, Index):
426+
return self.diff(other)
427+
elif isinstance(other, (datetools.DateOffset, timedelta)):
428+
new_values = self.astype('O') - other
429+
return DatetimeIndex(new_values, tz=self.tz, freq=self.freq)
424430
else:
425431
return Index(self.view(np.ndarray) + other)
426432

0 commit comments

Comments
 (0)