Skip to content

BUG: preserve frequency across Timestamp addition/subtraction (#4547) #6560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)


Expand Down
14 changes: 14 additions & 0 deletions pandas/tseries/tests/test_tslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 3 additions & 3 deletions pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down