Skip to content

Commit ad230e8

Browse files
jbrockmendeljreback
authored andcommitted
finish off tests.tseries.test_timezones (#19739)
1 parent 6e37f87 commit ad230e8

File tree

3 files changed

+55
-108
lines changed

3 files changed

+55
-108
lines changed

pandas/tests/scalar/timestamp/test_unary_ops.py

+24
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import pytest
55
import pytz
66
from pytz import utc
7+
from dateutil.tz import gettz
78

89
import pandas.util.testing as tm
910
import pandas.util._test_decorators as td
1011

1112
from pandas.compat import PY3
13+
from pandas._libs import tslib
1214
from pandas._libs.tslibs.frequencies import _INVALID_FREQ_ERROR
1315
from pandas import Timestamp, NaT
1416

@@ -215,6 +217,28 @@ def test_replace_tzinfo(self):
215217
assert result_dt == result_pd
216218
assert result_dt == result_pd.to_pydatetime()
217219

220+
@pytest.mark.parametrize('tz, normalize', [
221+
(pytz.timezone('US/Eastern'), lambda x: x.tzinfo.normalize(x)),
222+
(gettz('US/Eastern'), lambda x: x)])
223+
def test_replace_across_dst(self, tz, normalize):
224+
# GH#18319 check that 1) timezone is correctly normalized and
225+
# 2) that hour is not incorrectly changed by this normalization
226+
ts_naive = Timestamp('2017-12-03 16:03:30')
227+
ts_aware = tslib._localize_pydatetime(ts_naive, tz)
228+
229+
# Preliminary sanity-check
230+
assert ts_aware == normalize(ts_aware)
231+
232+
# Replace across DST boundary
233+
ts2 = ts_aware.replace(month=6)
234+
235+
# Check that `replace` preserves hour literal
236+
assert (ts2.hour, ts2.minute) == (ts_aware.hour, ts_aware.minute)
237+
238+
# Check that post-replace object is appropriately normalized
239+
ts2b = normalize(ts2)
240+
assert ts2 == ts2b
241+
218242
# --------------------------------------------------------------
219243

220244
@td.skip_if_windows

pandas/tests/tseries/test_timezones.py

-108
This file was deleted.

pandas/tests/tslibs/test_timezones.py

+31
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytz
66
import dateutil.tz
77

8+
from pandas._libs import tslib
89
from pandas._libs.tslibs import timezones
910
from pandas import Timestamp
1011

@@ -35,3 +36,33 @@ def test_tzlocal():
3536
offset = dateutil.tz.tzlocal().utcoffset(datetime(2011, 1, 1))
3637
offset = offset.total_seconds() * 1000000000
3738
assert ts.value + offset == Timestamp('2011-01-01').value
39+
40+
41+
@pytest.mark.parametrize('eastern, localize', [
42+
(pytz.timezone('US/Eastern'), lambda tz, x: tz.localize(x)),
43+
(dateutil.tz.gettz('US/Eastern'), lambda tz, x: x.replace(tzinfo=tz))])
44+
def test_infer_tz(eastern, localize):
45+
utc = pytz.utc
46+
47+
start_naive = datetime(2001, 1, 1)
48+
end_naive = datetime(2009, 1, 1)
49+
50+
start = localize(eastern, start_naive)
51+
end = localize(eastern, end_naive)
52+
53+
assert (timezones.infer_tzinfo(start, end) is
54+
tslib._localize_pydatetime(start_naive, eastern).tzinfo)
55+
assert (timezones.infer_tzinfo(start, None) is
56+
tslib._localize_pydatetime(start_naive, eastern).tzinfo)
57+
assert (timezones.infer_tzinfo(None, end) is
58+
tslib._localize_pydatetime(end_naive, eastern).tzinfo)
59+
60+
start = utc.localize(start_naive)
61+
end = utc.localize(end_naive)
62+
assert timezones.infer_tzinfo(start, end) is utc
63+
64+
end = tslib._localize_pydatetime(end_naive, eastern)
65+
with pytest.raises(Exception):
66+
timezones.infer_tzinfo(start, end)
67+
with pytest.raises(Exception):
68+
timezones.infer_tzinfo(end, start)

0 commit comments

Comments
 (0)