Skip to content

Commit 6fde3ae

Browse files
committed
BUG: preserve frequency across Timestamp addition/subtraction (#4547)
1 parent 170377d commit 6fde3ae

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ Bug Fixes
218218
- Bug in ``DataFrame.to_stata`` that lead to data loss in certain cases, and could exported using the
219219
wrong data types and missing values (:issue:`6335`)
220220
- Inconsistent types in Timestamp addition/subtraction (:issue:`6543`)
221+
- Bug in preserving frequency across Timestamp addition/subtraction (:issue:`4547`)
221222
- Bug in indexing: empty list lookup caused ``IndexError`` exceptions (:issue:`6536`, :issue:`6551`)
222223

223224

pandas/tseries/tests/test_tslib.py

+14
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,20 @@ def test_addition_subtraction_types(self):
330330
self.assertEqual(type(timestamp_instance + timedelta64_instance), Timestamp)
331331
self.assertEqual(type(timestamp_instance - timedelta64_instance), Timestamp)
332332

333+
def test_addition_subtraction_preserve_frequency(self):
334+
timestamp_instance = date_range('2014-03-05', periods=1, freq='D')[0]
335+
timedelta_instance = datetime.timedelta(days=1)
336+
original_freq = timestamp_instance.freq
337+
self.assertEqual((timestamp_instance + 1).freq, original_freq)
338+
self.assertEqual((timestamp_instance - 1).freq, original_freq)
339+
self.assertEqual((timestamp_instance + timedelta_instance).freq, original_freq)
340+
self.assertEqual((timestamp_instance - timedelta_instance).freq, original_freq)
341+
342+
if not _np_version_under1p7:
343+
timedelta64_instance = np.timedelta64(1, 'D')
344+
self.assertEqual((timestamp_instance + timedelta64_instance).freq, original_freq)
345+
self.assertEqual((timestamp_instance - timedelta64_instance).freq, original_freq)
346+
333347
if __name__ == '__main__':
334348
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
335349
exit=False)

pandas/tslib.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -681,17 +681,17 @@ cdef class _Timestamp(datetime):
681681

682682
if is_timedelta64_object(other):
683683
other_int = other.astype('timedelta64[ns]').astype(int)
684-
return Timestamp(self.value + other_int, tz=self.tzinfo)
684+
return Timestamp(self.value + other_int, tz=self.tzinfo, offset=self.offset)
685685

686686
if is_integer_object(other):
687687
if self.offset is None:
688688
raise ValueError("Cannot add integral value to Timestamp "
689689
"without offset.")
690-
return Timestamp((self.offset * other).apply(self))
690+
return Timestamp((self.offset * other).apply(self), offset=self.offset)
691691

692692
if isinstance(other, timedelta) or hasattr(other, 'delta'):
693693
nanos = _delta_to_nanoseconds(other)
694-
return Timestamp(self.value + nanos, tz=self.tzinfo)
694+
return Timestamp(self.value + nanos, tz=self.tzinfo, offset=self.offset)
695695

696696
result = datetime.__add__(self, other)
697697
if isinstance(result, datetime):

0 commit comments

Comments
 (0)