diff --git a/pandas/_libs/tslibs/conversion.pyx b/pandas/_libs/tslibs/conversion.pyx index bf5429c39e8fe..67c2793e4bcef 100644 --- a/pandas/_libs/tslibs/conversion.pyx +++ b/pandas/_libs/tslibs/conversion.pyx @@ -4,7 +4,7 @@ from cython import Py_ssize_t import numpy as np cimport numpy as cnp -from numpy cimport int64_t, int32_t, ndarray +from numpy cimport uint8_t, int64_t, int32_t, ndarray cnp.import_array() import pytz @@ -535,6 +535,7 @@ cdef inline void localize_tso(_TSObject obj, tzinfo tz): int64_t[:] deltas int64_t local_val Py_ssize_t pos + str typ assert obj.tzinfo is None @@ -646,7 +647,7 @@ cdef inline int64_t[:] _tz_convert_dst(int64_t[:] values, tzinfo tz, if not is_tzlocal(tz): # get_dst_info cannot extract offsets from tzlocal because its # dependent on a datetime - trans, deltas, typ = get_dst_info(tz) + trans, deltas, _ = get_dst_info(tz) if not to_utc: # We add `offset` below instead of subtracting it deltas = -1 * np.array(deltas, dtype='i8') @@ -690,7 +691,7 @@ cdef inline int64_t _tz_convert_tzlocal_utc(int64_t val, tzinfo tz, """ cdef: npy_datetimestruct dts - int64_t result, delta + int64_t delta datetime dt dt64_to_dtstruct(val, &dts) @@ -879,18 +880,20 @@ def tz_localize_to_utc(ndarray[int64_t] vals, object tz, object ambiguous=None, localized : ndarray[int64_t] """ cdef: - ndarray[int64_t] trans int64_t[:] deltas, idx_shifted, idx_shifted_left, idx_shifted_right - ndarray ambiguous_array + ndarray[uint8_t, cast=True] ambiguous_array, both_nat, both_eq Py_ssize_t i, idx, pos, ntrans, n = len(vals) Py_ssize_t delta_idx_offset, delta_idx, pos_left, pos_right int64_t *tdata int64_t v, left, right, val, v_left, v_right, new_local, remaining_mins int64_t HOURS_NS = HOUR_SECONDS * 1000000000 - ndarray[int64_t] result, result_a, result_b, dst_hours + ndarray[int64_t] trans, result, result_a, result_b, dst_hours + ndarray[int64_t] trans_idx, grp, delta, a_idx, b_idx, one_diff npy_datetimestruct dts bint infer_dst = False, is_dst = False, fill = False bint shift = False, fill_nonexist = False + list trans_grp + str stamp # Vectorized version of DstTzInfo.localize if is_utc(tz) or tz is None: @@ -923,7 +926,7 @@ def tz_localize_to_utc(ndarray[int64_t] vals, object tz, object ambiguous=None, if len(ambiguous) != len(vals): raise ValueError("Length of ambiguous bool-array must be " "the same size as vals") - ambiguous_array = np.asarray(ambiguous) + ambiguous_array = np.asarray(ambiguous, dtype=bool) if nonexistent == 'NaT': fill_nonexist = True @@ -933,7 +936,7 @@ def tz_localize_to_utc(ndarray[int64_t] vals, object tz, object ambiguous=None, assert nonexistent in ('raise', None), ("nonexistent must be one of" " {'NaT', 'raise', 'shift'}") - trans, deltas, typ = get_dst_info(tz) + trans, deltas, _ = get_dst_info(tz) tdata = cnp.PyArray_DATA(trans) ntrans = len(trans) @@ -984,7 +987,7 @@ def tz_localize_to_utc(ndarray[int64_t] vals, object tz, object ambiguous=None, # years which is useful for checking that there is not an ambiguous # transition in an individual year. if trans_idx.size > 0: - one_diff = np.where(np.diff(trans_idx) != 1)[0] +1 + one_diff = np.where(np.diff(trans_idx) != 1)[0] + 1 trans_grp = np.array_split(trans_idx, one_diff) # Iterate through each day, if there are no hours where the @@ -1172,13 +1175,14 @@ cdef int64_t[:] _normalize_local(int64_t[:] stamps, tzinfo tz): result : int64 ndarray of converted of normalized nanosecond timestamps """ cdef: - Py_ssize_t n = len(stamps) + Py_ssize_t i, n = len(stamps) int64_t[:] result = np.empty(n, dtype=np.int64) ndarray[int64_t] trans int64_t[:] deltas + str typ Py_ssize_t[:] pos npy_datetimestruct dts - int64_t delta + int64_t delta, local_val if is_utc(tz): with nogil: @@ -1264,6 +1268,7 @@ def is_date_array_normalized(int64_t[:] stamps, object tz=None): int64_t[:] deltas npy_datetimestruct dts int64_t local_val, delta + str typ if tz is None or is_utc(tz): for i in range(n):