Skip to content

Commit

Permalink
BUG: Fix irregular Timestamp arithmetic types pandas-dev#6543
Browse files Browse the repository at this point in the history
  • Loading branch information
rosnfeld authored and gouthambs committed Mar 12, 2014
1 parent 17b5fd9 commit d5401cb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
3 changes: 2 additions & 1 deletion doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ Bug Fixes
- Bug in :meth:`DataFrame.replace` where nested dicts were erroneously
depending on the order of dictionary keys and values (:issue:`5338`).
- Perf issue in concatting with empty objects (:issue:`3259`)
- Clarify sorting of ``sym_diff`` on ``Index``es with ``NaN``s (:isssue:`6444`)
- Clarify sorting of ``sym_diff`` on ``Index``es with ``NaN``s (:issue:`6444`)
- Bug in ``str.split`` when passed ``pat=None`` and ``n=1`` (:issue:`6466`)
- Bug in ``io.data.DataReader`` when passed ``"F-F_Momentum_Factor"`` and ``data_source="famafrench"`` (:issue:`6460`)
- Bug in ``sum`` of a ``timedelta64[ns]`` series (:issue:`6462`)
Expand All @@ -215,6 +215,7 @@ Bug Fixes
- Bug in ``pd.read_stata`` which would use the wrong data types and missing values (:issue:`6327`)
- 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 indexing: empty list lookup caused ``IndexError`` exceptions (:issue:`6536`, :issue:`6551`)


Expand Down
29 changes: 26 additions & 3 deletions pandas/tseries/tests/test_tslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from pandas.core.api import Timestamp
from pandas.tslib import period_asfreq, period_ordinal
from pandas.tseries.index import date_range
from pandas.tseries.frequencies import get_freq
from pandas import _np_version_under1p7
import pandas.util.testing as tm
Expand Down Expand Up @@ -302,10 +303,32 @@ def test_period_ordinal_business_day(self):
# Tuesday
self.assertEqual(11418, period_ordinal(2013, 10, 8, 0, 0, 0, 0, 0, get_freq('B')))

class TestTomeStampOps(tm.TestCase):
class TestTimestampOps(tm.TestCase):
def test_timestamp_and_datetime(self):
self.assertEqual((Timestamp(datetime.datetime(2013, 10,13)) - datetime.datetime(2013, 10,12)).days, 1)
self.assertEqual((datetime.datetime(2013, 10, 12) - Timestamp(datetime.datetime(2013, 10,13))).days, -1)
self.assertEqual((Timestamp(datetime.datetime(2013, 10, 13)) - datetime.datetime(2013, 10, 12)).days, 1)
self.assertEqual((datetime.datetime(2013, 10, 12) - Timestamp(datetime.datetime(2013, 10, 13))).days, -1)

def test_addition_subtraction_types(self):
# Assert on the types resulting from Timestamp +/- various date/time objects
datetime_instance = datetime.datetime(2014, 3, 4)
timedelta_instance = datetime.timedelta(seconds=1)
# build a timestamp with a frequency, since then it supports addition/subtraction of integers
timestamp_instance = date_range(datetime_instance, periods=1, freq='D')[0]

self.assertEqual(type(timestamp_instance + 1), Timestamp)
self.assertEqual(type(timestamp_instance - 1), Timestamp)

# Timestamp + datetime not supported, though subtraction is supported and yields timedelta
self.assertEqual(type(timestamp_instance - datetime_instance), datetime.timedelta)

self.assertEqual(type(timestamp_instance + timedelta_instance), Timestamp)
self.assertEqual(type(timestamp_instance - timedelta_instance), Timestamp)

if not _np_version_under1p7:
# Timestamp +/- datetime64 not supported, so not tested (could possibly assert error raised?)
timedelta64_instance = np.timedelta64(1, 'D')
self.assertEqual(type(timestamp_instance + timedelta64_instance), Timestamp)
self.assertEqual(type(timestamp_instance - timedelta64_instance), Timestamp)

if __name__ == '__main__':
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
Expand Down
10 changes: 5 additions & 5 deletions pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -700,11 +700,11 @@ cdef class _Timestamp(datetime):
return result

def __sub__(self, other):
if is_integer_object(other):
neg_other = -other
return self + neg_other
# This calling convention is required
return datetime.__sub__(self, other)
if isinstance(other, datetime):
return datetime.__sub__(self, other)

neg_other = -other
return self + neg_other

cpdef _get_field(self, field):
out = get_date_field(np.array([self.value], dtype=np.int64), field)
Expand Down

0 comments on commit d5401cb

Please sign in to comment.