Skip to content
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

Last of the timezones funcs #17669

Merged
merged 14 commits into from
Sep 29, 2017
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
2 changes: 0 additions & 2 deletions pandas/_libs/tslibs/timezones.pxd
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# -*- coding: utf-8 -*-
# cython: profile=False

from numpy cimport ndarray

cdef bint is_utc(object tz)
cdef bint is_tzlocal(object tz)

Expand Down
19 changes: 19 additions & 0 deletions pandas/_libs/tslibs/timezones.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# -*- coding: utf-8 -*-
# cython: profile=False
# cython: linetrace=False
# distutils: define_macros=CYTHON_TRACE=0
# distutils: define_macros=CYTHON_TRACE_NOGIL=0

cimport cython
from cython cimport Py_ssize_t
Expand Down Expand Up @@ -275,3 +278,19 @@ cdef object get_dst_info(object tz):
dst_cache[cache_key] = (trans, deltas, typ)

return dst_cache[cache_key]


def infer_tzinfo(start, end):
if start is not None and end is not None:
tz = start.tzinfo
if end.tzinfo:
if not (get_timezone(tz) == get_timezone(end.tzinfo)):
msg = 'Inputs must both have the same timezone, {tz1} != {tz2}'
raise AssertionError(msg.format(tz1=tz, tz2=end.tzinfo))
elif start is not None:
tz = start.tzinfo
elif end is not None:
tz = end.tzinfo
else:
tz = None
return tz
2 changes: 1 addition & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def _generate(cls, start, end, periods, name, offset,
raise ValueError("Closed has to be either 'left', 'right' or None")

try:
inferred_tz = tools._infer_tzinfo(start, end)
inferred_tz = timezones.infer_tzinfo(start, end)
except:
raise TypeError('Start and end cannot both be tz-aware with '
'different timezones')
Expand Down
19 changes: 0 additions & 19 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from pandas._libs import tslib
from pandas._libs.tslibs.strptime import array_strptime
from pandas._libs.tslibs.timezones import get_timezone
from pandas._libs.tslibs import parsing
from pandas._libs.tslibs.parsing import ( # noqa
parse_time_string,
Expand All @@ -29,24 +28,6 @@
from pandas.core import algorithms


def _infer_tzinfo(start, end):
def _infer(a, b):
tz = a.tzinfo
if b and b.tzinfo:
Copy link
Contributor

Choose a reason for hiding this comment

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

where is this actually used?

Copy link
Member Author

Choose a reason for hiding this comment

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

Outside of tests, its used once in indexes.datetimes

if not (get_timezone(tz) == get_timezone(b.tzinfo)):
raise AssertionError('Inputs must both have the same timezone,'
' {timezone1} != {timezone2}'
.format(timezone1=tz, timezone2=b.tzinfo))
return tz

tz = None
if start is not None:
tz = _infer(start, end)
elif end is not None:
tz = _infer(end, start)
return tz


def _guess_datetime_format_for_array(arr, **kwargs):
# Try to guess the format based on the first non-NaN element
non_nan_elements = notna(arr).nonzero()[0]
Expand Down
19 changes: 9 additions & 10 deletions pandas/tests/tseries/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from datetime import datetime, timedelta, tzinfo, date

import pandas.util.testing as tm
import pandas.core.tools.datetimes as tools
import pandas.tseries.offsets as offsets
from pandas.compat import lrange, zip
from pandas.core.indexes.datetimes import bdate_range, date_range
Expand Down Expand Up @@ -646,20 +645,20 @@ def test_infer_tz(self):

start = self.localize(eastern, _start)
end = self.localize(eastern, _end)
assert (tools._infer_tzinfo(start, end) is self.localize(
eastern, _start).tzinfo)
assert (tools._infer_tzinfo(start, None) is self.localize(
eastern, _start).tzinfo)
assert (tools._infer_tzinfo(None, end) is self.localize(eastern,
_end).tzinfo)
assert (timezones.infer_tzinfo(start, end) is
self.localize(eastern, _start).tzinfo)
assert (timezones.infer_tzinfo(start, None) is
self.localize(eastern, _start).tzinfo)
assert (timezones.infer_tzinfo(None, end) is
self.localize(eastern, _end).tzinfo)

start = utc.localize(_start)
end = utc.localize(_end)
assert (tools._infer_tzinfo(start, end) is utc)
assert (timezones.infer_tzinfo(start, end) is utc)

end = self.localize(eastern, _end)
pytest.raises(Exception, tools._infer_tzinfo, start, end)
pytest.raises(Exception, tools._infer_tzinfo, end, start)
pytest.raises(Exception, timezones.infer_tzinfo, start, end)
pytest.raises(Exception, timezones.infer_tzinfo, end, start)

def test_tz_string(self):
result = date_range('1/1/2000', periods=10,
Expand Down