Skip to content

Commit 54f6648

Browse files
jbrockmendeljreback
authored andcommitted
Last of the timezones funcs (#17669)
1 parent ffa86c5 commit 54f6648

File tree

5 files changed

+29
-32
lines changed

5 files changed

+29
-32
lines changed

pandas/_libs/tslibs/timezones.pxd

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# cython: profile=False
33

4-
from numpy cimport ndarray
5-
64
cdef bint is_utc(object tz)
75
cdef bint is_tzlocal(object tz)
86

pandas/_libs/tslibs/timezones.pyx

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# -*- coding: utf-8 -*-
22
# cython: profile=False
3+
# cython: linetrace=False
4+
# distutils: define_macros=CYTHON_TRACE=0
5+
# distutils: define_macros=CYTHON_TRACE_NOGIL=0
36

47
cimport cython
58
from cython cimport Py_ssize_t
@@ -275,3 +278,19 @@ cdef object get_dst_info(object tz):
275278
dst_cache[cache_key] = (trans, deltas, typ)
276279

277280
return dst_cache[cache_key]
281+
282+
283+
def infer_tzinfo(start, end):
284+
if start is not None and end is not None:
285+
tz = start.tzinfo
286+
if end.tzinfo:
287+
if not (get_timezone(tz) == get_timezone(end.tzinfo)):
288+
msg = 'Inputs must both have the same timezone, {tz1} != {tz2}'
289+
raise AssertionError(msg.format(tz1=tz, tz2=end.tzinfo))
290+
elif start is not None:
291+
tz = start.tzinfo
292+
elif end is not None:
293+
tz = end.tzinfo
294+
else:
295+
tz = None
296+
return tz

pandas/core/indexes/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ def _generate(cls, start, end, periods, name, offset,
443443
raise ValueError("Closed has to be either 'left', 'right' or None")
444444

445445
try:
446-
inferred_tz = tools._infer_tzinfo(start, end)
446+
inferred_tz = timezones.infer_tzinfo(start, end)
447447
except:
448448
raise TypeError('Start and end cannot both be tz-aware with '
449449
'different timezones')

pandas/core/tools/datetimes.py

-19
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from pandas._libs import tslib
66
from pandas._libs.tslibs.strptime import array_strptime
7-
from pandas._libs.tslibs.timezones import get_timezone
87
from pandas._libs.tslibs import parsing
98
from pandas._libs.tslibs.parsing import ( # noqa
109
parse_time_string,
@@ -30,24 +29,6 @@
3029
from pandas.core import algorithms
3130

3231

33-
def _infer_tzinfo(start, end):
34-
def _infer(a, b):
35-
tz = a.tzinfo
36-
if b and b.tzinfo:
37-
if not (get_timezone(tz) == get_timezone(b.tzinfo)):
38-
raise AssertionError('Inputs must both have the same timezone,'
39-
' {timezone1} != {timezone2}'
40-
.format(timezone1=tz, timezone2=b.tzinfo))
41-
return tz
42-
43-
tz = None
44-
if start is not None:
45-
tz = _infer(start, end)
46-
elif end is not None:
47-
tz = _infer(end, start)
48-
return tz
49-
50-
5132
def _guess_datetime_format_for_array(arr, **kwargs):
5233
# Try to guess the format based on the first non-NaN element
5334
non_nan_elements = notna(arr).nonzero()[0]

pandas/tests/tseries/test_timezones.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from datetime import datetime, timedelta, tzinfo, date
1313

1414
import pandas.util.testing as tm
15-
import pandas.core.tools.datetimes as tools
1615
import pandas.tseries.offsets as offsets
1716
from pandas.compat import lrange, zip
1817
from pandas.core.indexes.datetimes import bdate_range, date_range
@@ -646,20 +645,20 @@ def test_infer_tz(self):
646645

647646
start = self.localize(eastern, _start)
648647
end = self.localize(eastern, _end)
649-
assert (tools._infer_tzinfo(start, end) is self.localize(
650-
eastern, _start).tzinfo)
651-
assert (tools._infer_tzinfo(start, None) is self.localize(
652-
eastern, _start).tzinfo)
653-
assert (tools._infer_tzinfo(None, end) is self.localize(eastern,
654-
_end).tzinfo)
648+
assert (timezones.infer_tzinfo(start, end) is
649+
self.localize(eastern, _start).tzinfo)
650+
assert (timezones.infer_tzinfo(start, None) is
651+
self.localize(eastern, _start).tzinfo)
652+
assert (timezones.infer_tzinfo(None, end) is
653+
self.localize(eastern, _end).tzinfo)
655654

656655
start = utc.localize(_start)
657656
end = utc.localize(_end)
658-
assert (tools._infer_tzinfo(start, end) is utc)
657+
assert (timezones.infer_tzinfo(start, end) is utc)
659658

660659
end = self.localize(eastern, _end)
661-
pytest.raises(Exception, tools._infer_tzinfo, start, end)
662-
pytest.raises(Exception, tools._infer_tzinfo, end, start)
660+
pytest.raises(Exception, timezones.infer_tzinfo, start, end)
661+
pytest.raises(Exception, timezones.infer_tzinfo, end, start)
663662

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

0 commit comments

Comments
 (0)