Skip to content

finish off tests.tseries.test_timezones #19739

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 4 commits into from
Feb 18, 2018
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
24 changes: 24 additions & 0 deletions pandas/tests/scalar/timestamp/test_unary_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import pytest
import pytz
from pytz import utc
from dateutil.tz import gettz

import pandas.util.testing as tm
import pandas.util._test_decorators as td

from pandas.compat import PY3
from pandas._libs import tslib
from pandas._libs.tslibs.frequencies import _INVALID_FREQ_ERROR
from pandas import Timestamp, NaT

Expand Down Expand Up @@ -215,6 +217,28 @@ def test_replace_tzinfo(self):
assert result_dt == result_pd
assert result_dt == result_pd.to_pydatetime()

@pytest.mark.parametrize('tz, normalize', [
(pytz.timezone('US/Eastern'), lambda x: x.tzinfo.normalize(x)),
(gettz('US/Eastern'), lambda x: x)])
def test_replace_across_dst(self, tz, normalize):
# GH#18319 check that 1) timezone is correctly normalized and
# 2) that hour is not incorrectly changed by this normalization
ts_naive = Timestamp('2017-12-03 16:03:30')
ts_aware = tslib._localize_pydatetime(ts_naive, tz)

# Preliminary sanity-check
assert ts_aware == normalize(ts_aware)

# Replace across DST boundary
ts2 = ts_aware.replace(month=6)

# Check that `replace` preserves hour literal
assert (ts2.hour, ts2.minute) == (ts_aware.hour, ts_aware.minute)

# Check that post-replace object is appropriately normalized
ts2b = normalize(ts2)
assert ts2 == ts2b

# --------------------------------------------------------------

@td.skip_if_windows
Expand Down
108 changes: 0 additions & 108 deletions pandas/tests/tseries/test_timezones.py

This file was deleted.

31 changes: 31 additions & 0 deletions pandas/tests/tslibs/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytz
import dateutil.tz

from pandas._libs import tslib
from pandas._libs.tslibs import timezones
from pandas import Timestamp

Expand Down Expand Up @@ -35,3 +36,33 @@ def test_tzlocal():
offset = dateutil.tz.tzlocal().utcoffset(datetime(2011, 1, 1))
offset = offset.total_seconds() * 1000000000
assert ts.value + offset == Timestamp('2011-01-01').value


@pytest.mark.parametrize('eastern, localize', [
(pytz.timezone('US/Eastern'), lambda tz, x: tz.localize(x)),
(dateutil.tz.gettz('US/Eastern'), lambda tz, x: x.replace(tzinfo=tz))])
def test_infer_tz(eastern, localize):
utc = pytz.utc

start_naive = datetime(2001, 1, 1)
end_naive = datetime(2009, 1, 1)

start = localize(eastern, start_naive)
end = localize(eastern, end_naive)

assert (timezones.infer_tzinfo(start, end) is
tslib._localize_pydatetime(start_naive, eastern).tzinfo)
assert (timezones.infer_tzinfo(start, None) is
tslib._localize_pydatetime(start_naive, eastern).tzinfo)
assert (timezones.infer_tzinfo(None, end) is
tslib._localize_pydatetime(end_naive, eastern).tzinfo)

start = utc.localize(start_naive)
end = utc.localize(end_naive)
assert timezones.infer_tzinfo(start, end) is utc

end = tslib._localize_pydatetime(end_naive, eastern)
with pytest.raises(Exception):
timezones.infer_tzinfo(start, end)
with pytest.raises(Exception):
timezones.infer_tzinfo(end, start)