Skip to content

Commit 45791f7

Browse files
rosnfeldjreback
authored andcommitted
BUG: Fix irregular Timestamp arithmetic types pandas-dev#6543
DOC: Adding item for fix to pandas-dev#6543
1 parent fc07edb commit 45791f7

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

doc/source/release.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Bug Fixes
182182
- Bug in :meth:`DataFrame.replace` where nested dicts were erroneously
183183
depending on the order of dictionary keys and values (:issue:`5338`).
184184
- Perf issue in concatting with empty objects (:issue:`3259`)
185-
- Clarify sorting of ``sym_diff`` on ``Index``es with ``NaN``s (:isssue:`6444`)
185+
- Clarify sorting of ``sym_diff`` on ``Index``es with ``NaN``s (:issue:`6444`)
186186
- Regression in ``MultiIndex.from_product`` with a ``DatetimeIndex`` as input (:issue:`6439`)
187187
- Bug in ``str.extract`` when passed a non-default index (:issue:`6348`)
188188
- Bug in ``str.split`` when passed ``pat=None`` and ``n=1`` (:issue:`6466`)
@@ -200,6 +200,7 @@ Bug Fixes
200200
- Regression from 0.13 in the treatmenet of numpy ``datetime64`` non-ns dtypes in Series creation (:issue:`6529`)
201201
- ``.names`` attribute of MultiIndexes passed to ``set_index`` are now preserved (:issue:`6459`).
202202
- Bug in setitem with a duplicate index and an alignable rhs (:issue:`6541`)
203+
- Inconsistent types in Timestamp addition/subtraction (:issue:`6543`)
203204

204205
pandas 0.13.1
205206
-------------

pandas/tseries/tests/test_tslib.py

+26-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from pandas.core.api import Timestamp
99
from pandas.tslib import period_asfreq, period_ordinal
10+
from pandas.tseries.index import date_range
1011
from pandas.tseries.frequencies import get_freq
1112
from pandas import _np_version_under1p7
1213
import pandas.util.testing as tm
@@ -302,10 +303,32 @@ def test_period_ordinal_business_day(self):
302303
# Tuesday
303304
self.assertEqual(11418, period_ordinal(2013, 10, 8, 0, 0, 0, 0, 0, get_freq('B')))
304305

305-
class TestTomeStampOps(tm.TestCase):
306+
class TestTimestampOps(tm.TestCase):
306307
def test_timestamp_and_datetime(self):
307-
self.assertEqual((Timestamp(datetime.datetime(2013, 10,13)) - datetime.datetime(2013, 10,12)).days, 1)
308-
self.assertEqual((datetime.datetime(2013, 10, 12) - Timestamp(datetime.datetime(2013, 10,13))).days, -1)
308+
self.assertEqual((Timestamp(datetime.datetime(2013, 10, 13)) - datetime.datetime(2013, 10, 12)).days, 1)
309+
self.assertEqual((datetime.datetime(2013, 10, 12) - Timestamp(datetime.datetime(2013, 10, 13))).days, -1)
310+
311+
def test_addition_subtraction_types(self):
312+
# Assert on the types resulting from Timestamp +/- various date/time objects
313+
datetime_instance = datetime.datetime(2014, 3, 4)
314+
timedelta_instance = datetime.timedelta(seconds=1)
315+
timedelta64_instance = np.timedelta64(1, 'D')
316+
# build a timestamp with a frequency, since then it supports addition/subtraction of integers
317+
timestamp_instance = date_range(datetime_instance, periods=1, freq='D')[0]
318+
319+
self.assertEqual(type(timestamp_instance + 1), Timestamp)
320+
self.assertEqual(type(timestamp_instance - 1), Timestamp)
321+
322+
# Timestamp + datetime not supported, though subtraction is supported and yields timedelta
323+
self.assertEqual(type(timestamp_instance - datetime_instance), datetime.timedelta)
324+
325+
# Timestamp +/- datetime64 not supported, so not tested (could possibly assert error thrown?)
326+
327+
self.assertEqual(type(timestamp_instance + timedelta_instance), Timestamp)
328+
self.assertEqual(type(timestamp_instance - timedelta_instance), Timestamp)
329+
330+
self.assertEqual(type(timestamp_instance + timedelta64_instance), Timestamp)
331+
self.assertEqual(type(timestamp_instance - timedelta64_instance), Timestamp)
309332

310333
if __name__ == '__main__':
311334
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

pandas/tslib.pyx

+5-5
Original file line numberDiff line numberDiff line change
@@ -700,11 +700,11 @@ cdef class _Timestamp(datetime):
700700
return result
701701

702702
def __sub__(self, other):
703-
if is_integer_object(other):
704-
neg_other = -other
705-
return self + neg_other
706-
# This calling convention is required
707-
return datetime.__sub__(self, other)
703+
if isinstance(other, datetime):
704+
return datetime.__sub__(self, other)
705+
706+
neg_other = -other
707+
return self + neg_other
708708

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

0 commit comments

Comments
 (0)