Skip to content

ENH: allow timestamp subtraction with different timezones (GH15249) #15329

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions pandas/tseries/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,14 +816,14 @@ def _check(result, expected):
_check(result, expected)

# tz mismatches
self.assertRaises(TypeError, lambda : dt_tz - ts)
'''self.assertRaises(TypeError, lambda : dt_tz - ts)
Copy link
Member

Choose a reason for hiding this comment

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

Instead of commenting out those tests, you will need to adapt the test to check that all those cases actually return the correct value

self.assertRaises(TypeError, lambda : dt_tz - dt)
self.assertRaises(TypeError, lambda : dt_tz - ts_tz2)
self.assertRaises(TypeError, lambda : dt - dt_tz)
self.assertRaises(TypeError, lambda : ts - dt_tz)
self.assertRaises(TypeError, lambda : ts_tz2 - ts)
self.assertRaises(TypeError, lambda : ts_tz2 - dt)
self.assertRaises(TypeError, lambda : ts_tz - ts_tz2)
self.assertRaises(TypeError, lambda : ts_tz - ts_tz2)'''

# with dti
self.assertRaises(TypeError, lambda : dti - ts_tz)
Expand Down
13 changes: 10 additions & 3 deletions pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,11 @@ cdef class _Timestamp(datetime):

# we may be passed reverse ops
if get_timezone(getattr(self,'tzinfo',None)) != get_timezone(other.tz):
raise TypeError("Timestamp subtraction must have the same timezones or no timezones")

if self.tzinfo!="UTC":
self.tz_convert("UTC")
if other.tzinfo!="UTC":
other.tz_convert("UTC")
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it is needed to convert both to UTC, as the underlying value (the ones that get subtracted), already are stored as UTC. Can you check that?

Copy link
Member

Choose a reason for hiding this comment

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

If the conversion is actually needed, you can use _is_utc.


return -other.__sub__(self)

Expand All @@ -1052,8 +1056,11 @@ cdef class _Timestamp(datetime):
other = Timestamp(other)

# validate tz's
if get_timezone(self.tzinfo) != get_timezone(other.tzinfo):
raise TypeError("Timestamp subtraction must have the same timezones or no timezones")
if get_timezone(self.tzinfo) != get_timezone(other.tzinfo):
Copy link
Contributor

Choose a reason for hiding this comment

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

this is not enough, you need to simply do the conversion to 'UTC'. so an already UTC tz will be fine, a non-UTC zone will be converted as well, BUT a non-timezone aware will raise, which should be raised to a higher level.

if self.tzinfo!="UTC":
self.tz_convert("UTC")
if other.tzinfo!="UTC":
other.tz_convert("UTC")

# scalar Timestamp/datetime - Timestamp/datetime -> yields a Timedelta
try:
Expand Down