Skip to content

Commit d43aba8

Browse files
jbrockmendeljreback
authored andcommitted
de-privatize timezone functions (#17543)
1 parent 87e2f54 commit d43aba8

15 files changed

+75
-74
lines changed

pandas/_libs/period.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ from lib cimport is_null_datetimelike
3434
from pandas._libs import tslib
3535
from pandas._libs.tslib import Timestamp, iNaT, NaT
3636
from tslibs.timezones cimport (
37-
is_utc, is_tzlocal, get_utcoffset, _get_dst_info, maybe_get_tz)
37+
is_utc, is_tzlocal, get_utcoffset, get_dst_info, maybe_get_tz)
3838
from tslib cimport _nat_scalar_rules
3939

4040
from tslibs.frequencies cimport get_freq_code
@@ -556,7 +556,7 @@ cdef _reso_local(ndarray[int64_t] stamps, object tz):
556556
reso = curr_reso
557557
else:
558558
# Adjust datetime64 timestamp, recompute datetimestruct
559-
trans, deltas, typ = _get_dst_info(tz)
559+
trans, deltas, typ = get_dst_info(tz)
560560

561561
_pos = trans.searchsorted(stamps, side='right') - 1
562562
if _pos.dtype != np.int64:
@@ -623,7 +623,7 @@ cdef ndarray[int64_t] localize_dt64arr_to_period(ndarray[int64_t] stamps,
623623
dts.us, dts.ps, freq)
624624
else:
625625
# Adjust datetime64 timestamp, recompute datetimestruct
626-
trans, deltas, typ = _get_dst_info(tz)
626+
trans, deltas, typ = get_dst_info(tz)
627627

628628
_pos = trans.searchsorted(stamps, side='right') - 1
629629
if _pos.dtype != np.int64:

pandas/_libs/tslib.pyx

+13-19
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,10 @@ iNaT = NPY_NAT
100100

101101

102102
from tslibs.timezones cimport (
103-
is_utc, is_tzlocal, _is_fixed_offset,
103+
is_utc, is_tzlocal, is_fixed_offset,
104104
treat_tz_as_dateutil, treat_tz_as_pytz,
105105
get_timezone, get_utcoffset, maybe_get_tz,
106-
_get_dst_info
107-
)
108-
from tslibs.timezones import ( # noqa
109-
get_timezone, get_utcoffset, maybe_get_tz,
110-
_p_tz_cache_key, dst_cache,
111-
_unbox_utcoffsets,
112-
_dateutil_gettz
106+
get_dst_info
113107
)
114108

115109

@@ -168,7 +162,7 @@ def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None, box=False):
168162
pandas_datetime_to_datetimestruct(
169163
value, PANDAS_FR_ns, &dts)
170164
result[i] = func_create(value, dts, tz, freq)
171-
elif is_tzlocal(tz) or _is_fixed_offset(tz):
165+
elif is_tzlocal(tz) or is_fixed_offset(tz):
172166
for i in range(n):
173167
value = arr[i]
174168
if value == NPY_NAT:
@@ -182,7 +176,7 @@ def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None, box=False):
182176
dt = Timestamp(dt)
183177
result[i] = dt
184178
else:
185-
trans, deltas, typ = _get_dst_info(tz)
179+
trans, deltas, typ = get_dst_info(tz)
186180

187181
for i in range(n):
188182

@@ -1641,12 +1635,12 @@ cdef inline void _localize_tso(_TSObject obj, object tz):
16411635
obj.tzinfo = tz
16421636
else:
16431637
# Adjust datetime64 timestamp, recompute datetimestruct
1644-
trans, deltas, typ = _get_dst_info(tz)
1638+
trans, deltas, typ = get_dst_info(tz)
16451639

16461640
pos = trans.searchsorted(obj.value, side='right') - 1
16471641

16481642
# static/pytz/dateutil specific code
1649-
if _is_fixed_offset(tz):
1643+
if is_fixed_offset(tz):
16501644
# statictzinfo
16511645
if len(deltas) > 0 and obj.value != NPY_NAT:
16521646
pandas_datetime_to_datetimestruct(obj.value + deltas[0],
@@ -4066,7 +4060,7 @@ def tz_convert(ndarray[int64_t] vals, object tz1, object tz2):
40664060
* 1000000000)
40674061
utc_dates[i] = v - delta
40684062
else:
4069-
trans, deltas, typ = _get_dst_info(tz1)
4063+
trans, deltas, typ = get_dst_info(tz1)
40704064

40714065
# all-NaT
40724066
tt = vals[vals!=NPY_NAT]
@@ -4108,7 +4102,7 @@ def tz_convert(ndarray[int64_t] vals, object tz1, object tz2):
41084102
return result
41094103

41104104
# Convert UTC to other timezone
4111-
trans, deltas, typ = _get_dst_info(tz2)
4105+
trans, deltas, typ = get_dst_info(tz2)
41124106

41134107
# use first non-NaT element
41144108
# if all-NaT, return all-NaT
@@ -4172,7 +4166,7 @@ cpdef int64_t tz_convert_single(int64_t val, object tz1, object tz2):
41724166
delta = int(get_utcoffset(tz1, dt).total_seconds()) * 1000000000
41734167
utc_date = val - delta
41744168
elif get_timezone(tz1) != 'UTC':
4175-
trans, deltas, typ = _get_dst_info(tz1)
4169+
trans, deltas, typ = get_dst_info(tz1)
41764170
pos = trans.searchsorted(val, side='right') - 1
41774171
if pos < 0:
41784172
raise ValueError('First time before start of DST info')
@@ -4191,7 +4185,7 @@ cpdef int64_t tz_convert_single(int64_t val, object tz1, object tz2):
41914185
return utc_date + delta
41924186

41934187
# Convert UTC to other timezone
4194-
trans, deltas, typ = _get_dst_info(tz2)
4188+
trans, deltas, typ = get_dst_info(tz2)
41954189

41964190
pos = trans.searchsorted(utc_date, side='right') - 1
41974191
if pos < 0:
@@ -4261,7 +4255,7 @@ def tz_localize_to_utc(ndarray[int64_t] vals, object tz, object ambiguous=None,
42614255
"Length of ambiguous bool-array must be the same size as vals")
42624256
ambiguous_array = np.asarray(ambiguous)
42634257

4264-
trans, deltas, typ = _get_dst_info(tz)
4258+
trans, deltas, typ = get_dst_info(tz)
42654259

42664260
tdata = <int64_t*> trans.data
42674261
ntrans = len(trans)
@@ -4967,7 +4961,7 @@ cdef _normalize_local(ndarray[int64_t] stamps, object tz):
49674961
result[i] = _normalized_stamp(&dts)
49684962
else:
49694963
# Adjust datetime64 timestamp, recompute datetimestruct
4970-
trans, deltas, typ = _get_dst_info(tz)
4964+
trans, deltas, typ = get_dst_info(tz)
49714965

49724966
_pos = trans.searchsorted(stamps, side='right') - 1
49734967
if _pos.dtype != np.int64:
@@ -5023,7 +5017,7 @@ def dates_normalized(ndarray[int64_t] stamps, tz=None):
50235017
if (dt.hour + dt.minute + dt.second + dt.microsecond) > 0:
50245018
return False
50255019
else:
5026-
trans, deltas, typ = _get_dst_info(tz)
5020+
trans, deltas, typ = get_dst_info(tz)
50275021

50285022
for i in range(n):
50295023
# Adjust datetime64 timestamp, recompute datetimestruct

pandas/_libs/tslibs/timezones.pxd

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ cpdef object get_timezone(object tz)
1313
cpdef object maybe_get_tz(object tz)
1414

1515
cpdef get_utcoffset(tzinfo, obj)
16-
cdef bint _is_fixed_offset(object tz)
16+
cdef bint is_fixed_offset(object tz)
1717

18-
cdef object _get_dst_info(object tz)
18+
cdef object get_dst_info(object tz)

pandas/_libs/tslibs/timezones.pyx

+13-13
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ from dateutil.tz import (
1313
import sys
1414
if sys.platform == 'win32' or sys.platform == 'cygwin':
1515
# equiv pd.compat.is_platform_windows()
16-
from dateutil.zoneinfo import gettz as _dateutil_gettz
16+
from dateutil.zoneinfo import gettz as dateutil_gettz
1717
else:
18-
from dateutil.tz import gettz as _dateutil_gettz
18+
from dateutil.tz import gettz as dateutil_gettz
1919

2020

2121
from pytz.tzinfo import BaseTzInfo as _pytz_BaseTzInfo
@@ -100,7 +100,7 @@ cpdef inline object maybe_get_tz(object tz):
100100
tz = _dateutil_tzlocal()
101101
elif tz.startswith('dateutil/'):
102102
zone = tz[9:]
103-
tz = _dateutil_gettz(zone)
103+
tz = dateutil_gettz(zone)
104104
# On Python 3 on Windows, the filename is not always set correctly.
105105
if isinstance(tz, _dateutil_tzfile) and '.tar.gz' in tz._filename:
106106
tz._filename = zone
@@ -113,14 +113,14 @@ cpdef inline object maybe_get_tz(object tz):
113113

114114
def _p_tz_cache_key(tz):
115115
""" Python interface for cache function to facilitate testing."""
116-
return _tz_cache_key(tz)
116+
return tz_cache_key(tz)
117117

118118

119119
# Timezone data caches, key is the pytz string or dateutil file name.
120120
dst_cache = {}
121121

122122

123-
cdef inline object _tz_cache_key(object tz):
123+
cdef inline object tz_cache_key(object tz):
124124
"""
125125
Return the key in the cache for the timezone info object or None
126126
if unknown.
@@ -163,7 +163,7 @@ cpdef get_utcoffset(tzinfo, obj):
163163
return tzinfo.utcoffset(obj)
164164

165165

166-
cdef inline bint _is_fixed_offset(object tz):
166+
cdef inline bint is_fixed_offset(object tz):
167167
if treat_tz_as_dateutil(tz):
168168
if len(tz._trans_idx) == 0 and len(tz._trans_list) == 0:
169169
return 1
@@ -178,7 +178,7 @@ cdef inline bint _is_fixed_offset(object tz):
178178
return 1
179179

180180

181-
cdef object _get_utc_trans_times_from_dateutil_tz(object tz):
181+
cdef object get_utc_trans_times_from_dateutil_tz(object tz):
182182
"""
183183
Transition times in dateutil timezones are stored in local non-dst
184184
time. This code converts them to UTC. It's the reverse of the code
@@ -193,7 +193,7 @@ cdef object _get_utc_trans_times_from_dateutil_tz(object tz):
193193
return new_trans
194194

195195

196-
cpdef ndarray _unbox_utcoffsets(object transinfo):
196+
cpdef ndarray unbox_utcoffsets(object transinfo):
197197
cdef:
198198
Py_ssize_t i, sz
199199
ndarray[int64_t] arr
@@ -211,15 +211,15 @@ cpdef ndarray _unbox_utcoffsets(object transinfo):
211211
# Daylight Savings
212212

213213

214-
cdef object _get_dst_info(object tz):
214+
cdef object get_dst_info(object tz):
215215
"""
216216
return a tuple of :
217217
(UTC times of DST transitions,
218218
UTC offsets in microseconds corresponding to DST transitions,
219219
string of type of transitions)
220220
221221
"""
222-
cache_key = _tz_cache_key(tz)
222+
cache_key = tz_cache_key(tz)
223223
if cache_key is None:
224224
num = int(get_utcoffset(tz, None).total_seconds()) * 1000000000
225225
return (np.array([NPY_NAT + 1], dtype=np.int64),
@@ -235,13 +235,13 @@ cdef object _get_dst_info(object tz):
235235
trans[0] = NPY_NAT + 1
236236
except Exception:
237237
pass
238-
deltas = _unbox_utcoffsets(tz._transition_info)
238+
deltas = unbox_utcoffsets(tz._transition_info)
239239
typ = 'pytz'
240240

241241
elif treat_tz_as_dateutil(tz):
242242
if len(tz._trans_list):
243243
# get utc trans times
244-
trans_list = _get_utc_trans_times_from_dateutil_tz(tz)
244+
trans_list = get_utc_trans_times_from_dateutil_tz(tz)
245245
trans = np.hstack([
246246
np.array([0], dtype='M8[s]'), # place holder for first item
247247
np.array(trans_list, dtype='M8[s]')]).astype(
@@ -255,7 +255,7 @@ cdef object _get_dst_info(object tz):
255255
deltas *= 1000000000
256256
typ = 'dateutil'
257257

258-
elif _is_fixed_offset(tz):
258+
elif is_fixed_offset(tz):
259259
trans = np.array([NPY_NAT + 1], dtype=np.int64)
260260
deltas = np.array([tz._ttinfo_std.offset],
261261
dtype='i8') * 1000000000

pandas/core/indexes/datetimes.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
from pandas._libs import (lib, index as libindex, tslib as libts,
5151
algos as libalgos, join as libjoin,
5252
Timestamp, period as libperiod)
53+
from pandas._libs.tslibs import timezones
5354

5455

5556
def _utc():
@@ -372,7 +373,7 @@ def __new__(cls, data=None,
372373
tz = subarr.tz
373374
else:
374375
if tz is not None:
375-
tz = libts.maybe_get_tz(tz)
376+
tz = timezones.maybe_get_tz(tz)
376377

377378
if (not isinstance(data, DatetimeIndex) or
378379
getattr(data, 'tz', None) is None):
@@ -447,17 +448,18 @@ def _generate(cls, start, end, periods, name, offset,
447448
raise TypeError('Start and end cannot both be tz-aware with '
448449
'different timezones')
449450

450-
inferred_tz = libts.maybe_get_tz(inferred_tz)
451+
inferred_tz = timezones.maybe_get_tz(inferred_tz)
451452

452453
# these may need to be localized
453-
tz = libts.maybe_get_tz(tz)
454+
tz = timezones.maybe_get_tz(tz)
454455
if tz is not None:
455456
date = start or end
456457
if date.tzinfo is not None and hasattr(tz, 'localize'):
457458
tz = tz.localize(date.replace(tzinfo=None)).tzinfo
458459

459460
if tz is not None and inferred_tz is not None:
460-
if not libts.get_timezone(inferred_tz) == libts.get_timezone(tz):
461+
if not (timezones.get_timezone(inferred_tz) ==
462+
timezones.get_timezone(tz)):
461463
raise AssertionError("Inferred time zone not equal to passed "
462464
"time zone")
463465

@@ -593,7 +595,7 @@ def _simple_new(cls, values, name=None, freq=None, tz=None,
593595
result._data = values
594596
result.name = name
595597
result.offset = freq
596-
result.tz = libts.maybe_get_tz(tz)
598+
result.tz = timezones.maybe_get_tz(tz)
597599
result._reset_identity()
598600
return result
599601

@@ -607,7 +609,7 @@ def tzinfo(self):
607609
@cache_readonly
608610
def _timezone(self):
609611
""" Comparable timezone both for pytz / dateutil"""
610-
return libts.get_timezone(self.tzinfo)
612+
return timezones.get_timezone(self.tzinfo)
611613

612614
def _has_same_tz(self, other):
613615
zzone = self._timezone
@@ -616,7 +618,7 @@ def _has_same_tz(self, other):
616618
if isinstance(other, np.datetime64):
617619
# convert to Timestamp as np.datetime64 doesn't have tz attr
618620
other = Timestamp(other)
619-
vzone = libts.get_timezone(getattr(other, 'tzinfo', '__no_tz__'))
621+
vzone = timezones.get_timezone(getattr(other, 'tzinfo', '__no_tz__'))
620622
return zzone == vzone
621623

622624
@classmethod
@@ -1779,7 +1781,7 @@ def tz_convert(self, tz):
17791781
TypeError
17801782
If DatetimeIndex is tz-naive.
17811783
"""
1782-
tz = libts.maybe_get_tz(tz)
1784+
tz = timezones.maybe_get_tz(tz)
17831785

17841786
if self.tz is None:
17851787
# tz naive, use tz_localize
@@ -1839,7 +1841,7 @@ def tz_localize(self, tz, ambiguous='raise', errors='raise'):
18391841
else:
18401842
raise TypeError("Already tz-aware, use tz_convert to convert.")
18411843
else:
1842-
tz = libts.maybe_get_tz(tz)
1844+
tz = timezones.maybe_get_tz(tz)
18431845
# Convert to UTC
18441846

18451847
new_dates = libts.tz_localize_to_utc(self.asi8, tz,

pandas/core/tools/datetimes.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from collections import MutableMapping
44

55
from pandas._libs import lib, tslib
6+
from pandas._libs.tslibs.timezones import get_timezone
67

78
from pandas.core.dtypes.common import (
89
_ensure_object,
@@ -44,7 +45,7 @@ def _infer_tzinfo(start, end):
4445
def _infer(a, b):
4546
tz = a.tzinfo
4647
if b and b.tzinfo:
47-
if not (tslib.get_timezone(tz) == tslib.get_timezone(b.tzinfo)):
48+
if not (get_timezone(tz) == get_timezone(b.tzinfo)):
4849
raise AssertionError('Inputs must both have the same timezone,'
4950
' {timezone1} != {timezone2}'
5051
.format(timezone1=tz, timezone2=b.tzinfo))

pandas/io/pytables.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
from pandas.core.config import get_option
4747
from pandas.core.computation.pytables import Expr, maybe_expression
4848

49-
from pandas._libs import tslib, algos, lib
49+
from pandas._libs import algos, lib
50+
from pandas._libs.tslibs import timezones
5051

5152
from distutils.version import LooseVersion
5253

@@ -4379,7 +4380,7 @@ def _get_info(info, name):
43794380

43804381
def _get_tz(tz):
43814382
""" for a tz-aware type, return an encoded zone """
4382-
zone = tslib.get_timezone(tz)
4383+
zone = timezones.get_timezone(tz)
43834384
if zone is None:
43844385
zone = tz.utcoffset().total_seconds()
43854386
return zone
@@ -4401,7 +4402,7 @@ def _set_tz(values, tz, preserve_UTC=False, coerce=False):
44014402
if tz is not None:
44024403
name = getattr(values, 'name', None)
44034404
values = values.ravel()
4404-
tz = tslib.get_timezone(_ensure_decoded(tz))
4405+
tz = timezones.get_timezone(_ensure_decoded(tz))
44054406
values = DatetimeIndex(values, name=name)
44064407
if values.tz is None:
44074408
values = values.tz_localize('UTC').tz_convert(tz)

pandas/tests/indexes/datetimes/test_date_range.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ def test_range_tz_dateutil(self):
394394
# see gh-2906
395395

396396
# Use maybe_get_tz to fix filename in tz under dateutil.
397-
from pandas._libs.tslib import maybe_get_tz
397+
from pandas._libs.tslibs.timezones import maybe_get_tz
398398
tz = lambda x: maybe_get_tz('dateutil/' + x)
399399

400400
start = datetime(2011, 1, 1, tzinfo=tz('US/Eastern'))

0 commit comments

Comments
 (0)