Skip to content

Commit 34cc2e8

Browse files
jbrockmendeljreback
authored andcommitted
1 parent 46856c3 commit 34cc2e8

File tree

6 files changed

+88
-67
lines changed

6 files changed

+88
-67
lines changed

pandas/_libs/period.pyx

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ from util cimport is_period_object, is_string_object
3333
from lib cimport is_null_datetimelike, is_period
3434
from pandas._libs import tslib, lib
3535
from pandas._libs.tslib import (Timedelta, Timestamp, iNaT,
36-
NaT, _get_utcoffset)
37-
from tslibs.timezones cimport _is_utc
36+
NaT)
37+
from tslibs.timezones cimport _is_utc, _is_tzlocal, _get_utcoffset
3838
from tslib cimport (
3939
maybe_get_tz,
40-
_is_tzlocal,
4140
_get_dst_info,
4241
_nat_scalar_rules)
4342

pandas/_libs/src/inference.pyx

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import sys
22
from decimal import Decimal
33
cimport util
44
cimport cython
5-
from tslib import NaT, get_timezone
5+
from tslib import NaT
6+
from tslibs.timezones cimport _get_zone
67
from datetime import datetime, timedelta
78
iNaT = util.get_nat()
89

@@ -900,13 +901,13 @@ cpdef bint is_datetime_with_singletz_array(ndarray[object] values):
900901
for i in range(n):
901902
base_val = values[i]
902903
if base_val is not NaT:
903-
base_tz = get_timezone(getattr(base_val, 'tzinfo', None))
904+
base_tz = _get_zone(getattr(base_val, 'tzinfo', None))
904905

905906
for j in range(i, n):
906907
val = values[j]
907908
if val is not NaT:
908909
tz = getattr(val, 'tzinfo', None)
909-
if base_tz != tz and base_tz != get_timezone(tz):
910+
if base_tz != tz and base_tz != _get_zone(tz):
910911
return False
911912
break
912913

pandas/_libs/tslib.pxd

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ from numpy cimport ndarray, int64_t
33
cdef convert_to_tsobject(object, object, object, bint, bint)
44
cpdef convert_to_timedelta64(object, object)
55
cpdef object maybe_get_tz(object)
6-
cdef bint _is_tzlocal(object)
76
cdef object _get_dst_info(object)
87
cdef bint _nat_scalar_rules[6]
98
cdef bint _check_all_nulls(obj)

pandas/_libs/tslib.pyx

+7-59
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,13 @@ cdef int64_t NPY_NAT = util.get_nat()
107107
iNaT = NPY_NAT
108108

109109

110-
from tslibs.timezones cimport _is_utc
110+
from tslibs.timezones cimport (
111+
_is_utc, _is_tzlocal,
112+
_treat_tz_as_dateutil, _treat_tz_as_pytz,
113+
_get_zone,
114+
_get_utcoffset)
115+
from tslibs.timezones import get_timezone, _get_utcoffset # noqa
116+
111117

112118
cdef inline object create_timestamp_from_ts(
113119
int64_t value, pandas_datetimestruct dts,
@@ -235,10 +241,6 @@ def ints_to_pytimedelta(ndarray[int64_t] arr, box=False):
235241
return result
236242

237243

238-
cdef inline bint _is_tzlocal(object tz):
239-
return isinstance(tz, _dateutil_tzlocal)
240-
241-
242244
cdef inline bint _is_fixed_offset(object tz):
243245
if _treat_tz_as_dateutil(tz):
244246
if len(tz._trans_idx) == 0 and len(tz._trans_list) == 0:
@@ -1443,11 +1445,6 @@ cdef class _TSObject:
14431445
def __get__(self):
14441446
return self.value
14451447

1446-
cpdef _get_utcoffset(tzinfo, obj):
1447-
try:
1448-
return tzinfo._utcoffset
1449-
except AttributeError:
1450-
return tzinfo.utcoffset(obj)
14511448

14521449
# helper to extract datetime and int64 from several different possibilities
14531450
cdef convert_to_tsobject(object ts, object tz, object unit,
@@ -1712,48 +1709,6 @@ def _localize_pydatetime(object dt, object tz):
17121709
return dt.replace(tzinfo=tz)
17131710

17141711

1715-
def get_timezone(tz):
1716-
return _get_zone(tz)
1717-
1718-
1719-
cdef inline object _get_zone(object tz):
1720-
"""
1721-
We need to do several things here:
1722-
1) Distinguish between pytz and dateutil timezones
1723-
2) Not be over-specific (e.g. US/Eastern with/without DST is same *zone*
1724-
but a different tz object)
1725-
3) Provide something to serialize when we're storing a datetime object
1726-
in pytables.
1727-
1728-
We return a string prefaced with dateutil if it's a dateutil tz, else just
1729-
the tz name. It needs to be a string so that we can serialize it with
1730-
UJSON/pytables. maybe_get_tz (below) is the inverse of this process.
1731-
"""
1732-
if _is_utc(tz):
1733-
return 'UTC'
1734-
else:
1735-
if _treat_tz_as_dateutil(tz):
1736-
if '.tar.gz' in tz._filename:
1737-
raise ValueError(
1738-
'Bad tz filename. Dateutil on python 3 on windows has a '
1739-
'bug which causes tzfile._filename to be the same for all '
1740-
'timezone files. Please construct dateutil timezones '
1741-
'implicitly by passing a string like "dateutil/Europe'
1742-
'/London" when you construct your pandas objects instead '
1743-
'of passing a timezone object. See '
1744-
'https://github.com/pandas-dev/pandas/pull/7362')
1745-
return 'dateutil/' + tz._filename
1746-
else:
1747-
# tz is a pytz timezone or unknown.
1748-
try:
1749-
zone = tz.zone
1750-
if zone is None:
1751-
return tz
1752-
return zone
1753-
except AttributeError:
1754-
return tz
1755-
1756-
17571712
cpdef inline object maybe_get_tz(object tz):
17581713
"""
17591714
(Maybe) Construct a timezone object from a string. If tz is a string, use
@@ -4285,13 +4240,6 @@ def tz_convert_single(int64_t val, object tz1, object tz2):
42854240
# Timezone data caches, key is the pytz string or dateutil file name.
42864241
dst_cache = {}
42874242

4288-
cdef inline bint _treat_tz_as_pytz(object tz):
4289-
return hasattr(tz, '_utc_transition_times') and hasattr(
4290-
tz, '_transition_info')
4291-
4292-
cdef inline bint _treat_tz_as_dateutil(object tz):
4293-
return hasattr(tz, '_trans_list') and hasattr(tz, '_trans_idx')
4294-
42954243

42964244
def _p_tz_cache_key(tz):
42974245
""" Python interface for cache function to facilitate testing."""

pandas/_libs/tslibs/timezones.pxd

+8
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,11 @@
22
# cython: profile=False
33

44
cdef bint _is_utc(object tz)
5+
cdef bint _is_tzlocal(object tz)
6+
7+
cdef bint _treat_tz_as_pytz(object tz)
8+
cdef bint _treat_tz_as_dateutil(object tz)
9+
10+
cdef object _get_zone(object tz)
11+
12+
cpdef _get_utcoffset(tzinfo, obj)

pandas/_libs/tslibs/timezones.pyx

+67-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,77 @@
22
# cython: profile=False
33

44
# dateutil compat
5-
from dateutil.tz import tzutc as _dateutil_tzutc
5+
from dateutil.tz import (
6+
tzutc as _dateutil_tzutc,
7+
tzlocal as _dateutil_tzlocal)
68

79
import pytz
810
UTC = pytz.utc
911

1012

1113
cdef inline bint _is_utc(object tz):
1214
return tz is UTC or isinstance(tz, _dateutil_tzutc)
15+
16+
17+
cdef inline bint _is_tzlocal(object tz):
18+
return isinstance(tz, _dateutil_tzlocal)
19+
20+
21+
cdef inline bint _treat_tz_as_pytz(object tz):
22+
return hasattr(tz, '_utc_transition_times') and hasattr(
23+
tz, '_transition_info')
24+
25+
26+
cdef inline bint _treat_tz_as_dateutil(object tz):
27+
return hasattr(tz, '_trans_list') and hasattr(tz, '_trans_idx')
28+
29+
30+
cdef inline object _get_zone(object tz):
31+
"""
32+
We need to do several things here:
33+
1) Distinguish between pytz and dateutil timezones
34+
2) Not be over-specific (e.g. US/Eastern with/without DST is same *zone*
35+
but a different tz object)
36+
3) Provide something to serialize when we're storing a datetime object
37+
in pytables.
38+
39+
We return a string prefaced with dateutil if it's a dateutil tz, else just
40+
the tz name. It needs to be a string so that we can serialize it with
41+
UJSON/pytables. maybe_get_tz (below) is the inverse of this process.
42+
"""
43+
if _is_utc(tz):
44+
return 'UTC'
45+
else:
46+
if _treat_tz_as_dateutil(tz):
47+
if '.tar.gz' in tz._filename:
48+
raise ValueError(
49+
'Bad tz filename. Dateutil on python 3 on windows has a '
50+
'bug which causes tzfile._filename to be the same for all '
51+
'timezone files. Please construct dateutil timezones '
52+
'implicitly by passing a string like "dateutil/Europe'
53+
'/London" when you construct your pandas objects instead '
54+
'of passing a timezone object. See '
55+
'https://github.com/pandas-dev/pandas/pull/7362')
56+
return 'dateutil/' + tz._filename
57+
else:
58+
# tz is a pytz timezone or unknown.
59+
try:
60+
zone = tz.zone
61+
if zone is None:
62+
return tz
63+
return zone
64+
except AttributeError:
65+
return tz
66+
67+
68+
def get_timezone(tz):
69+
return _get_zone(tz)
70+
71+
#----------------------------------------------------------------------
72+
# UTC Offsets
73+
74+
cpdef _get_utcoffset(tzinfo, obj):
75+
try:
76+
return tzinfo._utcoffset
77+
except AttributeError:
78+
return tzinfo.utcoffset(obj)

0 commit comments

Comments
 (0)