From 18f843027839891ac4e7247ce673dcf7af19dcc7 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 15 May 2020 11:02:01 -0700 Subject: [PATCH 1/3] CLN: use public functions --- pandas/_libs/tslib.pyx | 8 ++++++-- pandas/_libs/tslibs/conversion.pyx | 9 +++++---- pandas/_libs/tslibs/timezones.pxd | 3 +-- pandas/_libs/tslibs/timezones.pyx | 9 ++++----- pandas/_libs/tslibs/tzconversion.pxd | 1 - pandas/_libs/tslibs/util.pxd | 2 -- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index f6034a1347f90..aad5be7ad99a8 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -44,8 +44,12 @@ from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime from pandas._libs.tslibs.parsing import parse_datetime_string from pandas._libs.tslibs.timedeltas cimport cast_from_unit -from pandas._libs.tslibs.timezones cimport is_utc, is_tzlocal, get_dst_info -from pandas._libs.tslibs.timezones import UTC +from pandas._libs.tslibs.timezones cimport ( + get_dst_info, + is_utc, + is_tzlocal, + utc_pytz as UTC, +) from pandas._libs.tslibs.conversion cimport ( _TSObject, convert_datetime_to_tsobject, get_datetime64_nanos) diff --git a/pandas/_libs/tslibs/conversion.pyx b/pandas/_libs/tslibs/conversion.pyx index 7d8c85b6c8e0b..8f92b2438c51d 100644 --- a/pandas/_libs/tslibs/conversion.pyx +++ b/pandas/_libs/tslibs/conversion.pyx @@ -42,8 +42,9 @@ from pandas._libs.tslibs.nattype cimport ( from pandas._libs.tslibs.tzconversion import tz_localize_to_utc from pandas._libs.tslibs.tzconversion cimport ( - _tz_convert_tzlocal_utc, _tz_convert_tzlocal_fromutc, - tz_convert_single + tz_convert_utc_to_tzlocal, + _tz_convert_tzlocal_fromutc, + tz_convert_single, ) # ---------------------------------------------------------------------- @@ -745,7 +746,7 @@ cdef int64_t[:] _normalize_local(const int64_t[:] stamps, tzinfo tz): if stamps[i] == NPY_NAT: result[i] = NPY_NAT continue - local_val = _tz_convert_tzlocal_utc(stamps[i], tz, to_utc=False) + local_val = tz_convert_utc_to_tzlocal(stamps[i], tz) dt64_to_dtstruct(local_val, &dts) result[i] = _normalized_stamp(&dts) else: @@ -826,7 +827,7 @@ def is_date_array_normalized(const int64_t[:] stamps, object tz=None): return False elif is_tzlocal(tz): for i in range(n): - local_val = _tz_convert_tzlocal_utc(stamps[i], tz, to_utc=False) + local_val = tz_convert_utc_to_tzlocal(stamps[i], tz) dt64_to_dtstruct(local_val, &dts) if (dts.hour + dts.min + dts.sec + dts.us) > 0: return False diff --git a/pandas/_libs/tslibs/timezones.pxd b/pandas/_libs/tslibs/timezones.pxd index 2f2a60fe6edf5..14c0523787422 100644 --- a/pandas/_libs/tslibs/timezones.pxd +++ b/pandas/_libs/tslibs/timezones.pxd @@ -6,13 +6,12 @@ cpdef bint is_utc(object tz) cdef bint is_tzlocal(object tz) cdef bint treat_tz_as_pytz(object tz) -cdef bint treat_tz_as_dateutil(object tz) cpdef bint tz_compare(object start, object end) cpdef object get_timezone(object tz) cpdef object maybe_get_tz(object tz) cdef get_utcoffset(tzinfo tz, obj) -cdef bint is_fixed_offset(object tz) +cdef bint is_fixed_offset(tzinfo tz) cdef object get_dst_info(object tz) diff --git a/pandas/_libs/tslibs/timezones.pyx b/pandas/_libs/tslibs/timezones.pyx index 1edd5095b8749..7fbb50fcbfd41 100644 --- a/pandas/_libs/tslibs/timezones.pyx +++ b/pandas/_libs/tslibs/timezones.pyx @@ -3,14 +3,13 @@ from datetime import timezone # dateutil compat from dateutil.tz import ( + gettz as dateutil_gettz, tzfile as _dateutil_tzfile, tzlocal as _dateutil_tzlocal, tzutc as _dateutil_tzutc, ) -from dateutil.tz import gettz as dateutil_gettz - from pytz.tzinfo import BaseTzInfo as _pytz_BaseTzInfo import pytz UTC = pytz.utc @@ -161,7 +160,7 @@ cdef get_utcoffset(tzinfo tz, obj): return tz.utcoffset(obj) -cdef inline bint is_fixed_offset(object tz): +cdef inline bint is_fixed_offset(tzinfo tz): if treat_tz_as_dateutil(tz): if len(tz._trans_idx) == 0 and len(tz._trans_list) == 0: return 1 @@ -178,7 +177,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(tzinfo 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 @@ -240,7 +239,7 @@ cdef object get_dst_info(object tz): 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 1st item np.array(trans_list, dtype='M8[s]')]).astype( diff --git a/pandas/_libs/tslibs/tzconversion.pxd b/pandas/_libs/tslibs/tzconversion.pxd index c1dd88e5b2313..b91117e3e2a67 100644 --- a/pandas/_libs/tslibs/tzconversion.pxd +++ b/pandas/_libs/tslibs/tzconversion.pxd @@ -3,6 +3,5 @@ from numpy cimport int64_t cdef int64_t tz_convert_utc_to_tzlocal(int64_t utc_val, tzinfo tz) -cdef int64_t _tz_convert_tzlocal_utc(int64_t val, tzinfo tz, bint to_utc=*) cdef int64_t _tz_convert_tzlocal_fromutc(int64_t val, tzinfo tz, bint *fold) cpdef int64_t tz_convert_single(int64_t val, object tz1, object tz2) diff --git a/pandas/_libs/tslibs/util.pxd b/pandas/_libs/tslibs/util.pxd index eb535a994e538..90e0677fb0063 100644 --- a/pandas/_libs/tslibs/util.pxd +++ b/pandas/_libs/tslibs/util.pxd @@ -23,8 +23,6 @@ cdef extern from "Python.h": # thus they cannot be declared 'nogil'. Also PyUnicode_AsUTF8AndSize() can # potentially allocate memory inside in unlikely case of when underlying # unicode object was stored as non-utf8 and utf8 wasn't requested before. - bint PyBytes_AsStringAndSize(object obj, char** buf, - Py_ssize_t* length) except -1 const char* PyUnicode_AsUTF8AndSize(object obj, Py_ssize_t* length) except NULL From f47b829beebd602821d801a2f04ea8cb24fc5709 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 15 May 2020 12:33:41 -0700 Subject: [PATCH 2/3] cleanup docstring --- pandas/_libs/tslibs/conversion.pyx | 3 +-- pandas/_libs/tslibs/period.pyx | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/_libs/tslibs/conversion.pyx b/pandas/_libs/tslibs/conversion.pyx index 8f92b2438c51d..bcad7e127b492 100644 --- a/pandas/_libs/tslibs/conversion.pyx +++ b/pandas/_libs/tslibs/conversion.pyx @@ -706,8 +706,7 @@ def normalize_i8_timestamps(int64_t[:] stamps, object tz): result : int64 ndarray of converted of normalized nanosecond timestamps """ cdef: - Py_ssize_t n = len(stamps) - int64_t[:] result = np.empty(n, dtype=np.int64) + int64_t[:] result result = _normalize_local(stamps, tz) diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index c5be5b1d96469..44d2f273c3358 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -2194,7 +2194,7 @@ cdef class _Period(ABCPeriod): return (Period, object_state) def strftime(self, fmt: str) -> str: - """ + r""" Returns the string representation of the :class:`Period`, depending on the selected ``fmt``. ``fmt`` must be a string containing one or several directives. The method recognizes the same From 76bc3fe21da02cea10ef7b3fe30a0b3c154c2aeb Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 19 May 2020 08:35:15 -0700 Subject: [PATCH 3/3] revert r --- pandas/_libs/tslibs/period.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index 74f01b5dcb6d5..d28585f15e5b5 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -2193,7 +2193,7 @@ cdef class _Period: return (Period, object_state) def strftime(self, fmt: str) -> str: - r""" + """ Returns the string representation of the :class:`Period`, depending on the selected ``fmt``. ``fmt`` must be a string containing one or several directives. The method recognizes the same