diff --git a/pandas/_libs/tslibs/timezones.pxd b/pandas/_libs/tslibs/timezones.pxd index e5d1343e1c984..95e0474b3a174 100644 --- a/pandas/_libs/tslibs/timezones.pxd +++ b/pandas/_libs/tslibs/timezones.pxd @@ -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) diff --git a/pandas/_libs/tslibs/timezones.pyx b/pandas/_libs/tslibs/timezones.pyx index 48d82996a0bd0..7f778dde86e23 100644 --- a/pandas/_libs/tslibs/timezones.pyx +++ b/pandas/_libs/tslibs/timezones.pyx @@ -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 @@ -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 diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 39dc24642235b..9127864eab8a1 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -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') diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 8fe28aa400613..e335dfe3a4142 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -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, @@ -30,24 +29,6 @@ from pandas.core import algorithms -def _infer_tzinfo(start, end): - def _infer(a, b): - tz = a.tzinfo - if b and b.tzinfo: - 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] diff --git a/pandas/tests/tseries/test_timezones.py b/pandas/tests/tseries/test_timezones.py index e7b470e01e2af..aa8fe90ea6500 100644 --- a/pandas/tests/tseries/test_timezones.py +++ b/pandas/tests/tseries/test_timezones.py @@ -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 @@ -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,