From d77c181e467fd77536fe1b6b28c3123e103a7a52 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 15 Sep 2017 08:28:19 -0700 Subject: [PATCH 1/9] de-privatize timezone functions --- pandas/_libs/period.pyx | 6 ++--- pandas/_libs/tslib.pyx | 32 +++++++++++++------------- pandas/_libs/tslibs/timezones.pxd | 4 ++-- pandas/_libs/tslibs/timezones.pyx | 22 +++++++++--------- pandas/tests/tseries/test_timezones.py | 2 +- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/pandas/_libs/period.pyx b/pandas/_libs/period.pyx index 49353f7b0491c..785bc945c264b 100644 --- a/pandas/_libs/period.pyx +++ b/pandas/_libs/period.pyx @@ -35,7 +35,7 @@ from pandas._libs import tslib, lib from pandas._libs.tslib import (Timedelta, Timestamp, iNaT, NaT) from tslibs.timezones cimport ( - is_utc, is_tzlocal, get_utcoffset, _get_dst_info, maybe_get_tz) + is_utc, is_tzlocal, get_utcoffset, get_dst_info, maybe_get_tz) from tslib cimport _nat_scalar_rules from tslibs.frequencies cimport get_freq_code @@ -557,7 +557,7 @@ cdef _reso_local(ndarray[int64_t] stamps, object tz): reso = curr_reso else: # Adjust datetime64 timestamp, recompute datetimestruct - trans, deltas, typ = _get_dst_info(tz) + trans, deltas, typ = get_dst_info(tz) _pos = trans.searchsorted(stamps, side='right') - 1 if _pos.dtype != np.int64: @@ -624,7 +624,7 @@ cdef ndarray[int64_t] localize_dt64arr_to_period(ndarray[int64_t] stamps, dts.us, dts.ps, freq) else: # Adjust datetime64 timestamp, recompute datetimestruct - trans, deltas, typ = _get_dst_info(tz) + trans, deltas, typ = get_dst_info(tz) _pos = trans.searchsorted(stamps, side='right') - 1 if _pos.dtype != np.int64: diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index ec12611ae7f02..0e7d505a41709 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -100,16 +100,16 @@ iNaT = NPY_NAT from tslibs.timezones cimport ( - is_utc, is_tzlocal, _is_fixed_offset, + is_utc, is_tzlocal, is_fixed_offset, treat_tz_as_dateutil, treat_tz_as_pytz, get_timezone, get_utcoffset, maybe_get_tz, - _get_dst_info + get_dst_info ) from tslibs.timezones import ( # noqa get_timezone, get_utcoffset, maybe_get_tz, - _p_tz_cache_key, dst_cache, - _unbox_utcoffsets, - _dateutil_gettz + p_tz_cache_key, dst_cache, + unbox_utcoffsets, + dateutil_gettz ) @@ -167,7 +167,7 @@ def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None, box=False): pandas_datetime_to_datetimestruct( value, PANDAS_FR_ns, &dts) result[i] = func_create(value, dts, tz, freq) - elif is_tzlocal(tz) or _is_fixed_offset(tz): + elif is_tzlocal(tz) or is_fixed_offset(tz): for i in range(n): value = arr[i] if value == NPY_NAT: @@ -181,7 +181,7 @@ def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None, box=False): dt = Timestamp(dt) result[i] = dt else: - trans, deltas, typ = _get_dst_info(tz) + trans, deltas, typ = get_dst_info(tz) for i in range(n): @@ -1641,12 +1641,12 @@ cdef inline void _localize_tso(_TSObject obj, object tz): obj.tzinfo = tz else: # Adjust datetime64 timestamp, recompute datetimestruct - trans, deltas, typ = _get_dst_info(tz) + trans, deltas, typ = get_dst_info(tz) pos = trans.searchsorted(obj.value, side='right') - 1 # static/pytz/dateutil specific code - if _is_fixed_offset(tz): + if is_fixed_offset(tz): # statictzinfo if len(deltas) > 0 and obj.value != NPY_NAT: pandas_datetime_to_datetimestruct(obj.value + deltas[0], @@ -4066,7 +4066,7 @@ def tz_convert(ndarray[int64_t] vals, object tz1, object tz2): * 1000000000) utc_dates[i] = v - delta else: - trans, deltas, typ = _get_dst_info(tz1) + trans, deltas, typ = get_dst_info(tz1) # all-NaT tt = vals[vals!=NPY_NAT] @@ -4108,7 +4108,7 @@ def tz_convert(ndarray[int64_t] vals, object tz1, object tz2): return result # Convert UTC to other timezone - trans, deltas, typ = _get_dst_info(tz2) + trans, deltas, typ = get_dst_info(tz2) # use first non-NaT element # if all-NaT, return all-NaT @@ -4172,7 +4172,7 @@ def tz_convert_single(int64_t val, object tz1, object tz2): delta = int(get_utcoffset(tz1, dt).total_seconds()) * 1000000000 utc_date = val - delta elif get_timezone(tz1) != 'UTC': - trans, deltas, typ = _get_dst_info(tz1) + trans, deltas, typ = get_dst_info(tz1) pos = trans.searchsorted(val, side='right') - 1 if pos < 0: raise ValueError('First time before start of DST info') @@ -4191,7 +4191,7 @@ def tz_convert_single(int64_t val, object tz1, object tz2): return utc_date + delta # Convert UTC to other timezone - trans, deltas, typ = _get_dst_info(tz2) + trans, deltas, typ = get_dst_info(tz2) pos = trans.searchsorted(utc_date, side='right') - 1 if pos < 0: @@ -4261,7 +4261,7 @@ def tz_localize_to_utc(ndarray[int64_t] vals, object tz, object ambiguous=None, "Length of ambiguous bool-array must be the same size as vals") ambiguous_array = np.asarray(ambiguous) - trans, deltas, typ = _get_dst_info(tz) + trans, deltas, typ = get_dst_info(tz) tdata = trans.data ntrans = len(trans) @@ -4967,7 +4967,7 @@ cdef _normalize_local(ndarray[int64_t] stamps, object tz): result[i] = _normalized_stamp(&dts) else: # Adjust datetime64 timestamp, recompute datetimestruct - trans, deltas, typ = _get_dst_info(tz) + trans, deltas, typ = get_dst_info(tz) _pos = trans.searchsorted(stamps, side='right') - 1 if _pos.dtype != np.int64: @@ -5022,7 +5022,7 @@ def dates_normalized(ndarray[int64_t] stamps, tz=None): if (dt.hour + dt.minute + dt.second + dt.microsecond) > 0: return False else: - trans, deltas, typ = _get_dst_info(tz) + trans, deltas, typ = get_dst_info(tz) for i in range(n): # Adjust datetime64 timestamp, recompute datetimestruct diff --git a/pandas/_libs/tslibs/timezones.pxd b/pandas/_libs/tslibs/timezones.pxd index fac0018a78bc2..e5d1343e1c984 100644 --- a/pandas/_libs/tslibs/timezones.pxd +++ b/pandas/_libs/tslibs/timezones.pxd @@ -13,6 +13,6 @@ cpdef object get_timezone(object tz) cpdef object maybe_get_tz(object tz) cpdef get_utcoffset(tzinfo, obj) -cdef bint _is_fixed_offset(object tz) +cdef bint is_fixed_offset(object tz) -cdef object _get_dst_info(object tz) +cdef object get_dst_info(object tz) diff --git a/pandas/_libs/tslibs/timezones.pyx b/pandas/_libs/tslibs/timezones.pyx index 346da41e7073b..e356d2e25de4d 100644 --- a/pandas/_libs/tslibs/timezones.pyx +++ b/pandas/_libs/tslibs/timezones.pyx @@ -111,16 +111,16 @@ cpdef inline object maybe_get_tz(object tz): return tz -def _p_tz_cache_key(tz): +def p_tz_cache_key(tz): """ Python interface for cache function to facilitate testing.""" - return _tz_cache_key(tz) + return tz_cache_key(tz) # Timezone data caches, key is the pytz string or dateutil file name. dst_cache = {} -cdef inline object _tz_cache_key(object tz): +cdef inline object tz_cache_key(object tz): """ Return the key in the cache for the timezone info object or None if unknown. @@ -163,7 +163,7 @@ cpdef get_utcoffset(tzinfo, obj): return tzinfo.utcoffset(obj) -cdef inline bint _is_fixed_offset(object tz): +cdef inline bint is_fixed_offset(object tz): if treat_tz_as_dateutil(tz): if len(tz._trans_idx) == 0 and len(tz._trans_list) == 0: return 1 @@ -178,7 +178,7 @@ cdef inline bint _is_fixed_offset(object tz): return 1 -cdef object _get_utc_trans_times_from_dateutil_tz(object tz): +cdef object get_utc_trans_times_from_dateutil_tz(object tz): """ Transition times in dateutil timezones are stored in local non-dst 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): return new_trans -cpdef ndarray _unbox_utcoffsets(object transinfo): +cpdef ndarray unbox_utcoffsets(object transinfo): cdef: Py_ssize_t i, sz ndarray[int64_t] arr @@ -211,7 +211,7 @@ cpdef ndarray _unbox_utcoffsets(object transinfo): # Daylight Savings -cdef object _get_dst_info(object tz): +cdef object get_dst_info(object tz): """ return a tuple of : (UTC times of DST transitions, @@ -219,7 +219,7 @@ cdef object _get_dst_info(object tz): string of type of transitions) """ - cache_key = _tz_cache_key(tz) + cache_key = tz_cache_key(tz) if cache_key is None: num = int(get_utcoffset(tz, None).total_seconds()) * 1000000000 return (np.array([NPY_NAT + 1], dtype=np.int64), @@ -235,13 +235,13 @@ cdef object _get_dst_info(object tz): trans[0] = NPY_NAT + 1 except Exception: pass - deltas = _unbox_utcoffsets(tz._transition_info) + deltas = unbox_utcoffsets(tz._transition_info) typ = 'pytz' elif treat_tz_as_dateutil(tz): if len(tz._trans_list): # get utc trans times - trans_list = _get_utc_trans_times_from_dateutil_tz(tz) + trans_list = get_utc_trans_times_from_dateutil_tz(tz) trans = np.hstack([ np.array([0], dtype='M8[s]'), # place holder for first item np.array(trans_list, dtype='M8[s]')]).astype( @@ -255,7 +255,7 @@ cdef object _get_dst_info(object tz): deltas *= 1000000000 typ = 'dateutil' - elif _is_fixed_offset(tz): + elif is_fixed_offset(tz): trans = np.array([NPY_NAT + 1], dtype=np.int64) deltas = np.array([tz._ttinfo_std.offset], dtype='i8') * 1000000000 diff --git a/pandas/tests/tseries/test_timezones.py b/pandas/tests/tseries/test_timezones.py index a9ecfd797a32b..29feea0290a43 100644 --- a/pandas/tests/tseries/test_timezones.py +++ b/pandas/tests/tseries/test_timezones.py @@ -1181,7 +1181,7 @@ def test_cache_keys_are_distinct_for_pytz_vs_dateutil(self): if tz_d is None: # skip timezones that dateutil doesn't know about. continue - assert tslib._p_tz_cache_key(tz_p) != tslib._p_tz_cache_key(tz_d) + assert tslib.p_tz_cache_key(tz_p) != tslib.p_tz_cache_key(tz_d) class TestTimeZones(object): From 3f45e6167185afbb3a6e20034c14dc6b93c3a5ca Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 15 Sep 2017 10:47:57 -0700 Subject: [PATCH 2/9] fixup typo --- pandas/_libs/tslibs/timezones.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/tslibs/timezones.pyx b/pandas/_libs/tslibs/timezones.pyx index e356d2e25de4d..27bcf013a7f8f 100644 --- a/pandas/_libs/tslibs/timezones.pyx +++ b/pandas/_libs/tslibs/timezones.pyx @@ -13,9 +13,9 @@ from dateutil.tz import ( import sys if sys.platform == 'win32' or sys.platform == 'cygwin': # equiv pd.compat.is_platform_windows() - from dateutil.zoneinfo import gettz as _dateutil_gettz + from dateutil.zoneinfo import gettz as dateutil_gettz else: - from dateutil.tz import gettz as _dateutil_gettz + from dateutil.tz import gettz as dateutil_gettz from pytz.tzinfo import BaseTzInfo as _pytz_BaseTzInfo From 4b33f331d155236353bfad83e30eac24bfd0bc81 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Fri, 15 Sep 2017 22:58:40 -0700 Subject: [PATCH 3/9] typo fixup nameerror --- pandas/_libs/tslibs/timezones.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/timezones.pyx b/pandas/_libs/tslibs/timezones.pyx index 27bcf013a7f8f..367094595a9a2 100644 --- a/pandas/_libs/tslibs/timezones.pyx +++ b/pandas/_libs/tslibs/timezones.pyx @@ -100,7 +100,7 @@ cpdef inline object maybe_get_tz(object tz): tz = _dateutil_tzlocal() elif tz.startswith('dateutil/'): zone = tz[9:] - tz = _dateutil_gettz(zone) + tz = dateutil_gettz(zone) # On Python 3 on Windows, the filename is not always set correctly. if isinstance(tz, _dateutil_tzfile) and '.tar.gz' in tz._filename: tz._filename = zone From acf6dbd57b5623b99ac9511174ac423584fe888d Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 16 Sep 2017 10:55:26 -0700 Subject: [PATCH 4/9] Fix de-privatized import --- pandas/tests/indexes/datetimes/test_setops.py | 2 +- pandas/tests/scalar/test_period.py | 4 ++-- pandas/tests/scalar/test_timestamp.py | 2 +- pandas/tests/series/test_indexing.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/tests/indexes/datetimes/test_setops.py b/pandas/tests/indexes/datetimes/test_setops.py index f43c010f59b9e..f381f5cd06a9c 100644 --- a/pandas/tests/indexes/datetimes/test_setops.py +++ b/pandas/tests/indexes/datetimes/test_setops.py @@ -325,7 +325,7 @@ def test_month_range_union_tz_pytz(self): def test_month_range_union_tz_dateutil(self): tm._skip_if_windows_python_3() - from pandas._libs.tslib import _dateutil_gettz as timezone + from pandas._libs.tslib import dateutil_gettz as timezone tz = timezone('US/Eastern') early_start = datetime(2011, 1, 1) diff --git a/pandas/tests/scalar/test_period.py b/pandas/tests/scalar/test_period.py index a167c9c738b0b..f65c4d358388e 100644 --- a/pandas/tests/scalar/test_period.py +++ b/pandas/tests/scalar/test_period.py @@ -245,7 +245,7 @@ def test_timestamp_tz_arg(self): assert p.tz == exp.tz def test_timestamp_tz_arg_dateutil(self): - from pandas._libs.tslib import _dateutil_gettz as gettz + from pandas._libs.tslib import dateutil_gettz as gettz from pandas._libs.tslib import maybe_get_tz for case in ['dateutil/Europe/Brussels', 'dateutil/Asia/Tokyo', 'dateutil/US/Pacific']: @@ -264,7 +264,7 @@ def test_timestamp_tz_arg_dateutil(self): assert p.tz == exp.tz def test_timestamp_tz_arg_dateutil_from_string(self): - from pandas._libs.tslib import _dateutil_gettz as gettz + from pandas._libs.tslib import dateutil_gettz as gettz p = Period('1/1/2005', freq='M').to_timestamp(tz='dateutil/Europe/Brussels') assert p.tz == gettz('Europe/Brussels') diff --git a/pandas/tests/scalar/test_timestamp.py b/pandas/tests/scalar/test_timestamp.py index 8d47ce4802ac6..ba34f19ab4ff4 100644 --- a/pandas/tests/scalar/test_timestamp.py +++ b/pandas/tests/scalar/test_timestamp.py @@ -1295,7 +1295,7 @@ def test_timestamp_to_datetime_explicit_pytz(self): def test_timestamp_to_datetime_explicit_dateutil(self): tm._skip_if_windows_python_3() - from pandas._libs.tslib import _dateutil_gettz as gettz + from pandas._libs.tslib import dateutil_gettz as gettz rng = date_range('20090415', '20090519', tz=gettz('US/Eastern')) stamp = rng[0] diff --git a/pandas/tests/series/test_indexing.py b/pandas/tests/series/test_indexing.py index 45a92f6d6f50b..f1e5b916115bc 100644 --- a/pandas/tests/series/test_indexing.py +++ b/pandas/tests/series/test_indexing.py @@ -387,7 +387,7 @@ def test_getitem_setitem_datetime_tz_pytz(self): def test_getitem_setitem_datetime_tz_dateutil(self): from dateutil.tz import tzutc - from pandas._libs.tslib import _dateutil_gettz as gettz + from pandas._libs.tslib import dateutil_gettz as gettz tz = lambda x: tzutc() if x == 'UTC' else gettz( x) # handle special case for utc in dateutil From 5fa670e194e5df9b9ab27714348242c1d0a4b66a Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 16 Sep 2017 14:07:40 -0700 Subject: [PATCH 5/9] Import timezone funcs directly from tslibs.timezones --- pandas/_libs/tslib.pyx | 6 ----- pandas/core/indexes/datetimes.py | 21 +++++++++--------- pandas/core/tools/datetimes.py | 3 ++- pandas/io/pytables.py | 5 +++-- .../indexes/datetimes/test_date_range.py | 2 +- pandas/tests/indexes/datetimes/test_setops.py | 2 +- pandas/tests/io/test_pytables.py | 2 +- pandas/tests/scalar/test_period.py | 6 ++--- pandas/tests/scalar/test_timestamp.py | 4 ++-- pandas/tests/series/test_indexing.py | 2 +- pandas/tests/tseries/test_offsets.py | 3 ++- pandas/tests/tseries/test_timezones.py | 22 ++++++++++--------- 12 files changed, 39 insertions(+), 39 deletions(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 0e7d505a41709..ee8a03bd0c37c 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -105,12 +105,6 @@ from tslibs.timezones cimport ( get_timezone, get_utcoffset, maybe_get_tz, get_dst_info ) -from tslibs.timezones import ( # noqa - get_timezone, get_utcoffset, maybe_get_tz, - p_tz_cache_key, dst_cache, - unbox_utcoffsets, - dateutil_gettz - ) cdef inline object create_timestamp_from_ts( diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 1c8d0b334b91c..58ffe4805e80b 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -50,7 +50,7 @@ from pandas._libs import (lib, index as libindex, tslib as libts, algos as libalgos, join as libjoin, Timestamp, period as libperiod) - +from pandas._libs.tslibs import timezones def _utc(): import pytz @@ -372,7 +372,7 @@ def __new__(cls, data=None, tz = subarr.tz else: if tz is not None: - tz = libts.maybe_get_tz(tz) + tz = timezones.maybe_get_tz(tz) if (not isinstance(data, DatetimeIndex) or getattr(data, 'tz', None) is None): @@ -447,17 +447,18 @@ def _generate(cls, start, end, periods, name, offset, raise TypeError('Start and end cannot both be tz-aware with ' 'different timezones') - inferred_tz = libts.maybe_get_tz(inferred_tz) + inferred_tz = timezones.maybe_get_tz(inferred_tz) # these may need to be localized - tz = libts.maybe_get_tz(tz) + tz = timezones.maybe_get_tz(tz) if tz is not None: date = start or end if date.tzinfo is not None and hasattr(tz, 'localize'): tz = tz.localize(date.replace(tzinfo=None)).tzinfo if tz is not None and inferred_tz is not None: - if not libts.get_timezone(inferred_tz) == libts.get_timezone(tz): + if not (timezones.get_timezone(inferred_tz) == + timezones.get_timezone(tz)): raise AssertionError("Inferred time zone not equal to passed " "time zone") @@ -593,7 +594,7 @@ def _simple_new(cls, values, name=None, freq=None, tz=None, result._data = values result.name = name result.offset = freq - result.tz = libts.maybe_get_tz(tz) + result.tz = timezones.maybe_get_tz(tz) result._reset_identity() return result @@ -607,7 +608,7 @@ def tzinfo(self): @cache_readonly def _timezone(self): """ Comparable timezone both for pytz / dateutil""" - return libts.get_timezone(self.tzinfo) + return timezones.get_timezone(self.tzinfo) def _has_same_tz(self, other): zzone = self._timezone @@ -616,7 +617,7 @@ def _has_same_tz(self, other): if isinstance(other, np.datetime64): # convert to Timestamp as np.datetime64 doesn't have tz attr other = Timestamp(other) - vzone = libts.get_timezone(getattr(other, 'tzinfo', '__no_tz__')) + vzone = timezones.get_timezone(getattr(other, 'tzinfo', '__no_tz__')) return zzone == vzone @classmethod @@ -1779,7 +1780,7 @@ def tz_convert(self, tz): TypeError If DatetimeIndex is tz-naive. """ - tz = libts.maybe_get_tz(tz) + tz = timezones.maybe_get_tz(tz) if self.tz is None: # tz naive, use tz_localize @@ -1839,7 +1840,7 @@ def tz_localize(self, tz, ambiguous='raise', errors='raise'): else: raise TypeError("Already tz-aware, use tz_convert to convert.") else: - tz = libts.maybe_get_tz(tz) + tz = timezones.maybe_get_tz(tz) # Convert to UTC new_dates = libts.tz_localize_to_utc(self.asi8, tz, diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 9dde26f43ad33..95fe3ab83c2ab 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -3,6 +3,7 @@ from collections import MutableMapping from pandas._libs import lib, tslib +from pandas._libs.tslibs.timezones import get_timezone from pandas.core.dtypes.common import ( _ensure_object, @@ -44,7 +45,7 @@ def _infer_tzinfo(start, end): def _infer(a, b): tz = a.tzinfo if b and b.tzinfo: - if not (tslib.get_timezone(tz) == tslib.get_timezone(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)) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 9f819a4463bed..8bcb3ec7fb91c 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -47,6 +47,7 @@ from pandas.core.computation.pytables import Expr, maybe_expression from pandas._libs import tslib, algos, lib +from pandas._libs.tslibs import timezones from distutils.version import LooseVersion @@ -4379,7 +4380,7 @@ def _get_info(info, name): def _get_tz(tz): """ for a tz-aware type, return an encoded zone """ - zone = tslib.get_timezone(tz) + zone = timezones.get_timezone(tz) if zone is None: zone = tz.utcoffset().total_seconds() return zone @@ -4401,7 +4402,7 @@ def _set_tz(values, tz, preserve_UTC=False, coerce=False): if tz is not None: name = getattr(values, 'name', None) values = values.ravel() - tz = tslib.get_timezone(_ensure_decoded(tz)) + tz = timezones.get_timezone(_ensure_decoded(tz)) values = DatetimeIndex(values, name=name) if values.tz is None: values = values.tz_localize('UTC').tz_convert(tz) diff --git a/pandas/tests/indexes/datetimes/test_date_range.py b/pandas/tests/indexes/datetimes/test_date_range.py index 8d86bebdd4d5e..c373942cb4c63 100644 --- a/pandas/tests/indexes/datetimes/test_date_range.py +++ b/pandas/tests/indexes/datetimes/test_date_range.py @@ -394,7 +394,7 @@ def test_range_tz_dateutil(self): # see gh-2906 # Use maybe_get_tz to fix filename in tz under dateutil. - from pandas._libs.tslib import maybe_get_tz + from pandas._libs.tslibs.timezones import maybe_get_tz tz = lambda x: maybe_get_tz('dateutil/' + x) start = datetime(2011, 1, 1, tzinfo=tz('US/Eastern')) diff --git a/pandas/tests/indexes/datetimes/test_setops.py b/pandas/tests/indexes/datetimes/test_setops.py index f381f5cd06a9c..5fb694cfe2b7f 100644 --- a/pandas/tests/indexes/datetimes/test_setops.py +++ b/pandas/tests/indexes/datetimes/test_setops.py @@ -325,7 +325,7 @@ def test_month_range_union_tz_pytz(self): def test_month_range_union_tz_dateutil(self): tm._skip_if_windows_python_3() - from pandas._libs.tslib import dateutil_gettz as timezone + from pandas._libs.tslibs.timezones import dateutil_gettz as timezone tz = timezone('US/Eastern') early_start = datetime(2011, 1, 1) diff --git a/pandas/tests/io/test_pytables.py b/pandas/tests/io/test_pytables.py index f331378b654be..a05553d80c133 100644 --- a/pandas/tests/io/test_pytables.py +++ b/pandas/tests/io/test_pytables.py @@ -5421,7 +5421,7 @@ def test_append_with_timezones_dateutil(self): # use maybe_get_tz instead of dateutil.tz.gettz to handle the windows # filename issues. - from pandas._libs.tslib import maybe_get_tz + from pandas._libs.tslibs.timezones import maybe_get_tz gettz = lambda x: maybe_get_tz('dateutil/' + x) # as columns diff --git a/pandas/tests/scalar/test_period.py b/pandas/tests/scalar/test_period.py index f65c4d358388e..e2c8607d197a1 100644 --- a/pandas/tests/scalar/test_period.py +++ b/pandas/tests/scalar/test_period.py @@ -245,8 +245,8 @@ def test_timestamp_tz_arg(self): assert p.tz == exp.tz def test_timestamp_tz_arg_dateutil(self): - from pandas._libs.tslib import dateutil_gettz as gettz - from pandas._libs.tslib import maybe_get_tz + from pandas._libs.tslibs.timezones import dateutil_gettz as gettz + from pandas._libs.tslibs.timezones import maybe_get_tz for case in ['dateutil/Europe/Brussels', 'dateutil/Asia/Tokyo', 'dateutil/US/Pacific']: p = Period('1/1/2005', freq='M').to_timestamp( @@ -264,7 +264,7 @@ def test_timestamp_tz_arg_dateutil(self): assert p.tz == exp.tz def test_timestamp_tz_arg_dateutil_from_string(self): - from pandas._libs.tslib import dateutil_gettz as gettz + from pandas._libs.tslibs.timezones import dateutil_gettz as gettz p = Period('1/1/2005', freq='M').to_timestamp(tz='dateutil/Europe/Brussels') assert p.tz == gettz('Europe/Brussels') diff --git a/pandas/tests/scalar/test_timestamp.py b/pandas/tests/scalar/test_timestamp.py index ba34f19ab4ff4..c1b9f858a08de 100644 --- a/pandas/tests/scalar/test_timestamp.py +++ b/pandas/tests/scalar/test_timestamp.py @@ -17,7 +17,7 @@ import pandas.util.testing as tm from pandas.tseries import offsets, frequencies from pandas._libs import tslib, period -from pandas._libs.tslib import get_timezone +from pandas._libs.tslibs.timezones import get_timezone from pandas.compat import lrange, long from pandas.util.testing import assert_series_equal @@ -1295,7 +1295,7 @@ def test_timestamp_to_datetime_explicit_pytz(self): def test_timestamp_to_datetime_explicit_dateutil(self): tm._skip_if_windows_python_3() - from pandas._libs.tslib import dateutil_gettz as gettz + from pandas._libs.tslibs.timezones import dateutil_gettz as gettz rng = date_range('20090415', '20090519', tz=gettz('US/Eastern')) stamp = rng[0] diff --git a/pandas/tests/series/test_indexing.py b/pandas/tests/series/test_indexing.py index f1e5b916115bc..91187b709463a 100644 --- a/pandas/tests/series/test_indexing.py +++ b/pandas/tests/series/test_indexing.py @@ -387,7 +387,7 @@ def test_getitem_setitem_datetime_tz_pytz(self): def test_getitem_setitem_datetime_tz_dateutil(self): from dateutil.tz import tzutc - from pandas._libs.tslib import dateutil_gettz as gettz + from pandas._libs.tslibs.timezones import dateutil_gettz as gettz tz = lambda x: tzutc() if x == 'UTC' else gettz( x) # handle special case for utc in dateutil diff --git a/pandas/tests/tseries/test_offsets.py b/pandas/tests/tseries/test_offsets.py index cd2c29ffe3ac6..543d21e162f04 100644 --- a/pandas/tests/tseries/test_offsets.py +++ b/pandas/tests/tseries/test_offsets.py @@ -33,6 +33,7 @@ to_datetime, DateParseError) import pandas.tseries.offsets as offsets from pandas.io.pickle import read_pickle +from pandas._libs.tslibs import timezones from pandas._libs.tslib import normalize_date, NaT, Timestamp, Timedelta import pandas._libs.tslib as tslib import pandas.util.testing as tm @@ -288,7 +289,7 @@ def _check_offsetfunc_works(self, offset, funcname, dt, expected, for tz in self.timezones: expected_localize = expected.tz_localize(tz) - tz_obj = tslib.maybe_get_tz(tz) + tz_obj = timezones.maybe_get_tz(tz) dt_tz = tslib._localize_pydatetime(dt, tz_obj) result = func(dt_tz) diff --git a/pandas/tests/tseries/test_timezones.py b/pandas/tests/tseries/test_timezones.py index 29feea0290a43..9fbbadcc45c46 100644 --- a/pandas/tests/tseries/test_timezones.py +++ b/pandas/tests/tseries/test_timezones.py @@ -18,6 +18,7 @@ from pandas.core.indexes.datetimes import bdate_range, date_range from pandas.core.dtypes.dtypes import DatetimeTZDtype from pandas._libs import tslib +from pandas._libs.tslibs import timezones from pandas import (Index, Series, DataFrame, isna, Timestamp, NaT, DatetimeIndex, to_datetime) from pandas.util.testing import (assert_frame_equal, assert_series_equal, @@ -943,7 +944,7 @@ def tz(self, tz): Use tslib.maybe_get_tz so that we get the filename on the tz right on windows. See #7337. """ - return tslib.maybe_get_tz('dateutil/' + tz) + return timezones.maybe_get_tz('dateutil/' + tz) def tzstr(self, tz): """ Construct a timezone string from a string. Overridden in subclass @@ -962,7 +963,7 @@ def test_utc_with_system_utc(self): # Skipped on win32 due to dateutil bug tm._skip_if_windows() - from pandas._libs.tslib import maybe_get_tz + from pandas._libs.tslibs.timezones import maybe_get_tz # from system utc to real utc ts = Timestamp('2001-01-05 11:56', tz=maybe_get_tz('dateutil/UTC')) @@ -1133,7 +1134,7 @@ def test_tzlocal(self): assert ts.tz == dateutil.tz.tzlocal() assert "tz='tzlocal()')" in repr(ts) - tz = tslib.maybe_get_tz('tzlocal()') + tz = timezones.maybe_get_tz('tzlocal()') assert tz == dateutil.tz.tzlocal() # get offset using normal datetime for test @@ -1176,12 +1177,13 @@ def test_cache_keys_are_distinct_for_pytz_vs_dateutil(self): if tz_name == 'UTC': # skip utc as it's a special case in dateutil continue - tz_p = tslib.maybe_get_tz(tz_name) - tz_d = tslib.maybe_get_tz('dateutil/' + tz_name) + tz_p = timezones.maybe_get_tz(tz_name) + tz_d = timezones.maybe_get_tz('dateutil/' + tz_name) if tz_d is None: # skip timezones that dateutil doesn't know about. continue - assert tslib.p_tz_cache_key(tz_p) != tslib.p_tz_cache_key(tz_d) + assert (timezones.p_tz_cache_key(tz_p) != + timezones.p_tz_cache_key(tz_d)) class TestTimeZones(object): @@ -1743,13 +1745,13 @@ def compare_local_to_utc(tz_didx, utc_didx): # Check empty array result = tslib.tz_convert(np.array([], dtype=np.int64), - tslib.maybe_get_tz('US/Eastern'), - tslib.maybe_get_tz('Asia/Tokyo')) + timezones.maybe_get_tz('US/Eastern'), + timezones.maybe_get_tz('Asia/Tokyo')) tm.assert_numpy_array_equal(result, np.array([], dtype=np.int64)) # Check all-NaT array result = tslib.tz_convert(np.array([tslib.iNaT], dtype=np.int64), - tslib.maybe_get_tz('US/Eastern'), - tslib.maybe_get_tz('Asia/Tokyo')) + timezones.maybe_get_tz('US/Eastern'), + timezones.maybe_get_tz('Asia/Tokyo')) tm.assert_numpy_array_equal(result, np.array( [tslib.iNaT], dtype=np.int64)) From d77cbba2002af04051e683e8c1e0d3fb47e325bd Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sat, 16 Sep 2017 21:45:41 -0700 Subject: [PATCH 6/9] flake8 fixup remove unused import --- pandas/io/pytables.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 8bcb3ec7fb91c..4d300b200971a 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -46,7 +46,7 @@ from pandas.core.config import get_option from pandas.core.computation.pytables import Expr, maybe_expression -from pandas._libs import tslib, algos, lib +from pandas._libs import algos, lib from pandas._libs.tslibs import timezones from distutils.version import LooseVersion From 489815067c2205852a9c0d045d155a1ca548aecf Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sun, 17 Sep 2017 09:42:03 -0700 Subject: [PATCH 7/9] reprivative p_tz_cache_key, lint whitespace fixup --- pandas/_libs/tslibs/timezones.pyx | 2 +- pandas/core/indexes/datetimes.py | 1 + pandas/tests/tseries/test_timezones.py | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/_libs/tslibs/timezones.pyx b/pandas/_libs/tslibs/timezones.pyx index 367094595a9a2..48d82996a0bd0 100644 --- a/pandas/_libs/tslibs/timezones.pyx +++ b/pandas/_libs/tslibs/timezones.pyx @@ -111,7 +111,7 @@ cpdef inline object maybe_get_tz(object tz): return tz -def p_tz_cache_key(tz): +def _p_tz_cache_key(tz): """ Python interface for cache function to facilitate testing.""" return tz_cache_key(tz) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 58ffe4805e80b..1260d9547d5bb 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -52,6 +52,7 @@ Timestamp, period as libperiod) from pandas._libs.tslibs import timezones + def _utc(): import pytz return pytz.utc diff --git a/pandas/tests/tseries/test_timezones.py b/pandas/tests/tseries/test_timezones.py index 9fbbadcc45c46..ab0bb899d01e7 100644 --- a/pandas/tests/tseries/test_timezones.py +++ b/pandas/tests/tseries/test_timezones.py @@ -1182,8 +1182,8 @@ def test_cache_keys_are_distinct_for_pytz_vs_dateutil(self): if tz_d is None: # skip timezones that dateutil doesn't know about. continue - assert (timezones.p_tz_cache_key(tz_p) != - timezones.p_tz_cache_key(tz_d)) + assert (timezones._p_tz_cache_key(tz_p) != + timezones._p_tz_cache_key(tz_d)) class TestTimeZones(object): From b9454943dfadb60fb81765df81dd47404fc9579b Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sun, 17 Sep 2017 15:32:37 -0700 Subject: [PATCH 8/9] Dont alias dateutil_gettz --- pandas/tests/indexes/datetimes/test_setops.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexes/datetimes/test_setops.py b/pandas/tests/indexes/datetimes/test_setops.py index 5fb694cfe2b7f..4ffd2e1cd1e61 100644 --- a/pandas/tests/indexes/datetimes/test_setops.py +++ b/pandas/tests/indexes/datetimes/test_setops.py @@ -325,8 +325,8 @@ def test_month_range_union_tz_pytz(self): def test_month_range_union_tz_dateutil(self): tm._skip_if_windows_python_3() - from pandas._libs.tslibs.timezones import dateutil_gettz as timezone - tz = timezone('US/Eastern') + from pandas._libs.tslibs.timezones import dateutil_gettz + tz = dateutil_gettz('US/Eastern') early_start = datetime(2011, 1, 1) early_end = datetime(2011, 3, 1) From b6a14c2bf0f628993c0410646c6019a3d7315658 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sun, 17 Sep 2017 17:13:10 -0700 Subject: [PATCH 9/9] Remove aliasing for dateutil_gettz --- pandas/tests/scalar/test_period.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/tests/scalar/test_period.py b/pandas/tests/scalar/test_period.py index e2c8607d197a1..c17a216df44cb 100644 --- a/pandas/tests/scalar/test_period.py +++ b/pandas/tests/scalar/test_period.py @@ -245,7 +245,7 @@ def test_timestamp_tz_arg(self): assert p.tz == exp.tz def test_timestamp_tz_arg_dateutil(self): - from pandas._libs.tslibs.timezones import dateutil_gettz as gettz + from pandas._libs.tslibs.timezones import dateutil_gettz from pandas._libs.tslibs.timezones import maybe_get_tz for case in ['dateutil/Europe/Brussels', 'dateutil/Asia/Tokyo', 'dateutil/US/Pacific']: @@ -253,21 +253,21 @@ def test_timestamp_tz_arg_dateutil(self): tz=maybe_get_tz(case)) exp = Timestamp('1/1/2005', tz='UTC').tz_convert(case) assert p == exp - assert p.tz == gettz(case.split('/', 1)[1]) + assert p.tz == dateutil_gettz(case.split('/', 1)[1]) assert p.tz == exp.tz p = Period('1/1/2005', freq='M').to_timestamp(freq='3H', tz=maybe_get_tz(case)) exp = Timestamp('1/1/2005', tz='UTC').tz_convert(case) assert p == exp - assert p.tz == gettz(case.split('/', 1)[1]) + assert p.tz == dateutil_gettz(case.split('/', 1)[1]) assert p.tz == exp.tz def test_timestamp_tz_arg_dateutil_from_string(self): - from pandas._libs.tslibs.timezones import dateutil_gettz as gettz + from pandas._libs.tslibs.timezones import dateutil_gettz p = Period('1/1/2005', freq='M').to_timestamp(tz='dateutil/Europe/Brussels') - assert p.tz == gettz('Europe/Brussels') + assert p.tz == dateutil_gettz('Europe/Brussels') def test_timestamp_mult(self): p = pd.Period('2011-01', freq='M')