Skip to content

Commit 27abdcc

Browse files
committed
TST: 31739 - add coverage of subtracting datetimes w/differing timezones
1 parent 478410b commit 27abdcc

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

pandas/conftest.py

+32-19
Original file line numberDiff line numberDiff line change
@@ -1080,26 +1080,30 @@ def iris(datapath):
10801080
# ----------------------------------------------------------------
10811081
# Time zones
10821082
# ----------------------------------------------------------------
1083-
TIMEZONES = [
1084-
None,
1085-
"UTC",
1086-
"US/Eastern",
1087-
"Asia/Tokyo",
1088-
"dateutil/US/Pacific",
1089-
"dateutil/Asia/Singapore",
1090-
"+01:15",
1091-
"-02:15",
1092-
"UTC+01:15",
1093-
"UTC-02:15",
1094-
tzutc(),
1095-
tzlocal(),
1096-
FixedOffset(300),
1097-
FixedOffset(0),
1098-
FixedOffset(-300),
1099-
timezone.utc,
1100-
timezone(timedelta(hours=1)),
1101-
timezone(timedelta(hours=-1), name="foo"),
1083+
1084+
# contains a list of different timezones in a number of formats, along with the
1085+
# matched offset from UTC
1086+
TIMEZONE_MAPPING = [
1087+
(None, None),
1088+
("UTC", Timedelta("0 days")),
1089+
("US/Eastern", Timedelta("-5 hours")),
1090+
("Asia/Tokyo", Timedelta("9 hours")),
1091+
("dateutil/US/Pacific", Timedelta("-8 hours")),
1092+
("dateutil/Asia/Singapore", Timedelta("8 hours")),
1093+
("+01:15", Timedelta("1 hour 15 minutes")),
1094+
("-02:15", Timedelta("-2 hours 15 minutes")),
1095+
("UTC+01:15", Timedelta("1 hour 15 minutes")),
1096+
("UTC-02:15", Timedelta("-2 hours 15 minutes")),
1097+
(tzutc(), Timedelta("0 seconds")),
1098+
(tzlocal(), None),
1099+
(FixedOffset(300), Timedelta("300 minutes")),
1100+
(FixedOffset(0), Timedelta("0 minutes")),
1101+
(FixedOffset(-300), Timedelta("-300 minutes")),
1102+
(timezone.utc, Timedelta("0 seconds")),
1103+
(timezone(timedelta(hours=1)), Timedelta("1 hours")),
1104+
(timezone(timedelta(hours=-1), name="foo"), Timedelta("-1 hours")),
11021105
]
1106+
TIMEZONES = [i for (i, _) in TIMEZONE_MAPPING]
11031107
TIMEZONE_IDS = [repr(i) for i in TIMEZONES]
11041108

11051109

@@ -1121,6 +1125,15 @@ def tz_aware_fixture(request):
11211125
return request.param
11221126

11231127

1128+
@pytest.fixture
1129+
def tz_aware_utc_offset_fixture(tz_aware_fixture):
1130+
"""
1131+
Expected tz offset from UTC for each key in TIMEZONE_MAPPING
1132+
"""
1133+
tz, expected_offset = TIMEZONE_MAPPING[TIMEZONES.index(tz_aware_fixture)]
1134+
return expected_offset
1135+
1136+
11241137
# Generate cartesian product of tz_aware_fixture:
11251138
tz_aware_fixture2 = tz_aware_fixture
11261139

pandas/tests/scalar/timestamp/test_arithmetic.py

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
timezone,
55
)
66

7+
from dateutil.tz import tzlocal
78
import numpy as np
89
import pytest
910

@@ -141,6 +142,21 @@ def test_subtracting_involving_datetime_with_different_tz(self):
141142
assert isinstance(result, Timedelta)
142143
assert result == Timedelta("-1 days +18:00:00")
143144

145+
def test_subtracting_different_timezones(
146+
self, tz_aware_fixture, tz_aware_utc_offset_fixture
147+
):
148+
if tz_aware_fixture == tzlocal():
149+
pytest.skip("Subtraction won't work for naive tz")
150+
151+
t_raw = Timestamp("20130101")
152+
t_UTC = t_raw.tz_localize("UTC")
153+
t_diff = t_raw.tz_localize(tz_aware_fixture)
154+
155+
result = t_UTC - t_diff
156+
157+
assert isinstance(result, Timedelta)
158+
assert result == tz_aware_utc_offset_fixture
159+
144160
def test_addition_subtraction_types(self):
145161
# Assert on the types resulting from Timestamp +/- various date/time
146162
# objects

0 commit comments

Comments
 (0)