Skip to content

REGR: fixing Timestamp/Series subtraction, resolves #6648 #6657

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 18, 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
12 changes: 11 additions & 1 deletion pandas/tseries/tests/test_tslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
from pandas import tslib
import datetime

from pandas.core.api import Timestamp
from pandas.core.api import Timestamp, Series
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
from pandas.util.testing import assert_series_equal

class TestTimestamp(tm.TestCase):
def test_repr(self):
Expand Down Expand Up @@ -333,6 +334,15 @@ 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)

def test_timestamp_and_series(self):
timestamp_series = Series(date_range('2014-03-17', periods=2, freq='D', tz='US/Eastern'))
first_timestamp = timestamp_series[0]

if not _np_version_under1p7:
delta_series = Series([np.timedelta64(0, 'D'), np.timedelta64(1, 'D')])
assert_series_equal(timestamp_series - first_timestamp, delta_series)
assert_series_equal(first_timestamp - timestamp_series, -delta_series)

def test_addition_subtraction_types(self):
# Assert on the types resulting from Timestamp +/- various date/time objects
datetime_instance = datetime.datetime(2014, 3, 4)
Expand Down
9 changes: 5 additions & 4 deletions pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -707,11 +707,12 @@ cdef class _Timestamp(datetime):
return result

def __sub__(self, other):
if isinstance(other, datetime):
return datetime.__sub__(self, other)
if is_timedelta64_object(other) or is_integer_object(other) \
or isinstance(other, timedelta) or hasattr(other, 'delta'):
neg_other = -other
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of isinstance use is_timedelta_object
what has a delta attribute?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually the isinstance is ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I am just trying to copy the instance checks already in the __add__ code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh ok that's fine then

return self + neg_other

neg_other = -other
return self + neg_other
return datetime.__sub__(self, other)

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