Skip to content

Commit

Permalink
Merge pull request #6560 from rosnfeld/issue_4547
Browse files Browse the repository at this point in the history
BUG: preserve frequency across Timestamp addition/subtraction (#4547)
  • Loading branch information
jreback committed Mar 7, 2014
2 parents 7d49037 + 6fde3ae commit fb1b4a9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,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`)
- Series.quantile raising on an ``object`` dtype (:issue:`6555`)

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

0 comments on commit fb1b4a9

Please sign in to comment.