Skip to content

Commit 1c6d1e3

Browse files
[backport 3.0.x] PERF: remove special casing for zoneinfo in tz_localize_to_utc (#65481) (#65498)
1 parent 2a54711 commit 1c6d1e3

1 file changed

Lines changed: 13 additions & 105 deletions

File tree

‎pandas/_libs/tslibs/tzconversion.pyx‎

Lines changed: 13 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ from pandas._libs.tslibs.np_datetime cimport (
3636
import_pandas_datetime,
3737
npy_datetimestruct,
3838
pandas_datetime_to_datetimestruct,
39-
pydatetime_to_dt64,
4039
)
4140

4241
import_pandas_datetime()
@@ -47,7 +46,6 @@ from pandas._libs.tslibs.timezones cimport (
4746
is_tzlocal,
4847
is_utc,
4948
is_zoneinfo,
50-
utc_stdlib,
5149
)
5250

5351

@@ -240,15 +238,12 @@ timedelta-like}
240238
int64_t shift_delta = 0
241239
ndarray[int64_t] result_a, result_b, dst_hours
242240
int64_t[::1] result
243-
bint is_zi = False
244241
bint infer_dst = False, is_dst = False, fill = False
245242
bint shift_forward = False, shift_backward = False
246243
bint fill_nonexist = False
247244
str stamp
248245
Localizer info = Localizer(tz, creso=creso)
249246
int64_t pph = periods_per_day(creso) // 24
250-
int64_t pps = periods_per_second(creso)
251-
npy_datetimestruct dts
252247

253248
# Vectorized version of DstTzInfo.localize
254249

@@ -311,15 +306,9 @@ timedelta-like}
311306

312307
# Determine whether each date lies left of the DST transition (store in
313308
# result_a) or right of the DST transition (store in result_b)
314-
if is_zoneinfo(tz):
315-
is_zi = True
316-
result_a, result_b =_get_utc_bounds_zoneinfo(
317-
vals, tz, creso=creso
318-
)
319-
else:
320-
result_a, result_b =_get_utc_bounds(
321-
vals, info.tdata, info.ntrans, info.deltas, creso=creso
322-
)
309+
result_a, result_b =_get_utc_bounds(
310+
vals, info.tdata, info.ntrans, info.deltas, creso=creso
311+
)
323312

324313
# silence false-positive compiler warning
325314
dst_hours = np.empty(0, dtype=np.int64)
@@ -397,32 +386,17 @@ timedelta-like}
397386
# nonexistent times
398387
new_local = val - remaining_mins - 1
399388

400-
if is_zi:
401-
# use the same construction as in _get_utc_bounds_zoneinfo
402-
pandas_datetime_to_datetimestruct(new_local, creso, &dts)
403-
extra = (dts.ps // 1000) * (pps // 1_000_000_000)
404-
405-
dt = datetime_new(dts.year, dts.month, dts.day, dts.hour,
406-
dts.min, dts.sec, dts.us, None)
407-
408-
if shift_forward or shift_delta > 0:
409-
dt = dt.replace(tzinfo=tz, fold=1)
410-
else:
411-
dt = dt.replace(tzinfo=tz, fold=0)
412-
dt = dt.astimezone(utc_stdlib)
413-
dt = dt.replace(tzinfo=None)
414-
result[i] = pydatetime_to_dt64(dt, &dts, creso) + extra
415-
389+
delta_idx = bisect_right_i8(info.tdata, new_local, info.ntrans)
390+
# Logic similar to the precompute section. But check the current
391+
# delta in case we are moving between UTC+0 and non-zero timezone
392+
if (
393+
(shift_forward or shift_delta > 0)
394+
and info.deltas[delta_idx - 1] >= 0
395+
):
396+
delta_idx = delta_idx - 1
416397
else:
417-
delta_idx = bisect_right_i8(info.tdata, new_local, info.ntrans)
418-
# Logic similar to the precompute section. But check the current
419-
# delta in case we are moving between UTC+0 and non-zero timezone
420-
if (shift_forward or shift_delta > 0) and \
421-
info.deltas[delta_idx - 1] >= 0:
422-
delta_idx = delta_idx - 1
423-
else:
424-
delta_idx = delta_idx - delta_idx_offset
425-
result[i] = new_local - info.deltas[delta_idx]
398+
delta_idx = delta_idx - delta_idx_offset
399+
result[i] = new_local - info.deltas[delta_idx]
426400
elif fill_nonexist:
427401
result[i] = NPY_NAT
428402
else:
@@ -530,72 +504,6 @@ cdef _get_utc_bounds(
530504
return result_a, result_b
531505

532506

533-
cdef _get_utc_bounds_zoneinfo(ndarray vals, tz, NPY_DATETIMEUNIT creso):
534-
"""
535-
For each point in 'vals', find the UTC time that it corresponds to if
536-
with fold=0 and fold=1. In non-ambiguous cases, these will match.
537-
538-
Parameters
539-
----------
540-
vals : ndarray[int64_t]
541-
tz : ZoneInfo
542-
creso : NPY_DATETIMEUNIT
543-
544-
Returns
545-
-------
546-
ndarray[int64_t]
547-
ndarray[int64_t]
548-
"""
549-
cdef:
550-
Py_ssize_t i, n = vals.size
551-
npy_datetimestruct dts
552-
datetime dt, rt, left, right, aware, as_utc
553-
int64_t val, pps = periods_per_second(creso)
554-
ndarray result_a, result_b
555-
556-
result_a = cnp.PyArray_EMPTY(vals.ndim, vals.shape, cnp.NPY_INT64, 0)
557-
result_b = cnp.PyArray_EMPTY(vals.ndim, vals.shape, cnp.NPY_INT64, 0)
558-
559-
for i in range(n):
560-
val = vals[i]
561-
if val == NPY_NAT:
562-
result_a[i] = NPY_NAT
563-
result_b[i] = NPY_NAT
564-
continue
565-
566-
pandas_datetime_to_datetimestruct(val, creso, &dts)
567-
# casting to pydatetime drops nanoseconds etc, which we will
568-
# need to re-add later as 'extra'
569-
extra = (dts.ps // 1000) * (pps // 1_000_000_000)
570-
571-
dt = datetime_new(dts.year, dts.month, dts.day, dts.hour,
572-
dts.min, dts.sec, dts.us, None)
573-
574-
aware = dt.replace(tzinfo=tz)
575-
as_utc = aware.astimezone(utc_stdlib)
576-
rt = as_utc.astimezone(tz)
577-
if aware != rt:
578-
# AFAICT this means that 'aware' is non-existent
579-
# TODO: better way to check this?
580-
# mail.python.org/archives/list/[email protected]/
581-
# thread/57Y3IQAASJOKHX4D27W463XTZIS2NR3M/
582-
result_a[i] = NPY_NAT
583-
else:
584-
left = as_utc.replace(tzinfo=None)
585-
result_a[i] = pydatetime_to_dt64(left, &dts, creso) + extra
586-
587-
aware = dt.replace(fold=1, tzinfo=tz)
588-
as_utc = aware.astimezone(utc_stdlib)
589-
rt = as_utc.astimezone(tz)
590-
if aware != rt:
591-
result_b[i] = NPY_NAT
592-
else:
593-
right = as_utc.replace(tzinfo=None)
594-
result_b[i] = pydatetime_to_dt64(right, &dts, creso) + extra
595-
596-
return result_a, result_b
597-
598-
599507
@cython.boundscheck(False)
600508
cdef ndarray[int64_t] _get_dst_hours(
601509
# vals, creso only needed here to potential render an exception message

0 commit comments

Comments
 (0)