-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 4 commits
5f52a10
c6bcb06
2c5cbb6
cb47036
951ff1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the conversion is actually needed, you can use |
||
|
||
return -other.__sub__(self) | ||
|
||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
There was a problem hiding this comment.
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