From 6fde3ae98b1a52e41c411daf5a44fb340889f691 Mon Sep 17 00:00:00 2001 From: Andrew Rosenfeld Date: Wed, 5 Mar 2014 19:13:00 -0500 Subject: [PATCH] BUG: preserve frequency across Timestamp addition/subtraction (#4547) --- doc/source/release.rst | 1 + pandas/tseries/tests/test_tslib.py | 14 ++++++++++++++ pandas/tslib.pyx | 6 +++--- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/doc/source/release.rst b/doc/source/release.rst index 183d662f1578d..c273ae11aa1dc 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -218,6 +218,7 @@ Bug Fixes - Bug in ``DataFrame.to_stata`` that lead to data loss in certain cases, and could exported using the wrong data types and missing values (:issue:`6335`) - Inconsistent types in Timestamp addition/subtraction (:issue:`6543`) +- Bug in preserving frequency across Timestamp addition/subtraction (:issue:`4547`) - Bug in indexing: empty list lookup caused ``IndexError`` exceptions (:issue:`6536`, :issue:`6551`) diff --git a/pandas/tseries/tests/test_tslib.py b/pandas/tseries/tests/test_tslib.py index a24f545901ccd..b23b7b65825c5 100644 --- a/pandas/tseries/tests/test_tslib.py +++ b/pandas/tseries/tests/test_tslib.py @@ -330,6 +330,20 @@ def test_addition_subtraction_types(self): self.assertEqual(type(timestamp_instance + timedelta64_instance), Timestamp) self.assertEqual(type(timestamp_instance - timedelta64_instance), Timestamp) + def test_addition_subtraction_preserve_frequency(self): + timestamp_instance = date_range('2014-03-05', periods=1, freq='D')[0] + timedelta_instance = datetime.timedelta(days=1) + original_freq = timestamp_instance.freq + self.assertEqual((timestamp_instance + 1).freq, original_freq) + self.assertEqual((timestamp_instance - 1).freq, original_freq) + self.assertEqual((timestamp_instance + timedelta_instance).freq, original_freq) + self.assertEqual((timestamp_instance - timedelta_instance).freq, original_freq) + + if not _np_version_under1p7: + timedelta64_instance = np.timedelta64(1, 'D') + self.assertEqual((timestamp_instance + timedelta64_instance).freq, original_freq) + self.assertEqual((timestamp_instance - timedelta64_instance).freq, original_freq) + if __name__ == '__main__': nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], exit=False) diff --git a/pandas/tslib.pyx b/pandas/tslib.pyx index 9ff73e7c92fdb..da767b77d934c 100644 --- a/pandas/tslib.pyx +++ b/pandas/tslib.pyx @@ -681,17 +681,17 @@ cdef class _Timestamp(datetime): if is_timedelta64_object(other): other_int = other.astype('timedelta64[ns]').astype(int) - return Timestamp(self.value + other_int, tz=self.tzinfo) + return Timestamp(self.value + other_int, tz=self.tzinfo, offset=self.offset) if is_integer_object(other): if self.offset is None: raise ValueError("Cannot add integral value to Timestamp " "without offset.") - return Timestamp((self.offset * other).apply(self)) + return Timestamp((self.offset * other).apply(self), offset=self.offset) if isinstance(other, timedelta) or hasattr(other, 'delta'): nanos = _delta_to_nanoseconds(other) - return Timestamp(self.value + nanos, tz=self.tzinfo) + return Timestamp(self.value + nanos, tz=self.tzinfo, offset=self.offset) result = datetime.__add__(self, other) if isinstance(result, datetime):