@@ -15,7 +15,6 @@ from cpython.datetime cimport (
15
15
16
16
import_datetime()
17
17
18
- from dateutil.tz import tzutc
19
18
import numpy as np
20
19
import pytz
21
20
@@ -40,7 +39,6 @@ from pandas._libs.tslibs.np_datetime cimport (
40
39
)
41
40
from pandas._libs.tslibs.timezones cimport (
42
41
get_dst_info,
43
- get_utcoffset,
44
42
is_fixed_offset,
45
43
is_tzlocal,
46
44
is_utc,
@@ -194,6 +192,8 @@ timedelta-like}
194
192
result_b[:] = NPY_NAT
195
193
196
194
for i in range (n):
195
+ # This loops resembles the "Find the two best possibilities" block
196
+ # in pytz's DstTZInfo.localize method.
197
197
val = vals[i]
198
198
if val == NPY_NAT:
199
199
continue
@@ -339,27 +339,37 @@ cdef inline str _render_tstamp(int64_t val):
339
339
340
340
cdef ndarray[int64_t] _get_dst_hours(
341
341
# vals only needed here to potential render an exception message
342
- ndarray[ int64_t] vals,
342
+ const int64_t[: ] vals,
343
343
ndarray[int64_t] result_a,
344
344
ndarray[int64_t] result_b,
345
345
):
346
346
cdef:
347
- Py_ssize_t n = vals.shape[0 ]
348
- ndarray[uint8_t, cast= True ] both_nat, both_eq
347
+ Py_ssize_t i, n = vals.shape[0 ]
348
+ ndarray[uint8_t, cast= True ] mismatch
349
349
ndarray[int64_t] delta, dst_hours
350
- ndarray trans_idx, grp, a_idx, b_idx, one_diff
350
+ ndarray[intp_t] switch_idxs, trans_idx, grp, a_idx, b_idx, one_diff
351
351
list trans_grp
352
+ intp_t switch_idx
353
+ int64_t left, right
352
354
353
355
dst_hours = np.empty(n, dtype = np.int64)
354
356
dst_hours[:] = NPY_NAT
355
357
356
- # Get the ambiguous hours (given the above, these are the hours
357
- # where result_a != result_b and neither of them are NAT)
358
- both_nat = np.logical_and(result_a != NPY_NAT, result_b != NPY_NAT)
359
- both_eq = result_a == result_b
360
- trans_idx = np.squeeze(np.nonzero(np.logical_and(both_nat, ~ both_eq)))
358
+ mismatch = np.zeros(n, dtype = bool )
359
+
360
+ for i in range (n):
361
+ left = result_a[i]
362
+ right = result_b[i]
363
+
364
+ # Get the ambiguous hours (given the above, these are the hours
365
+ # where result_a != result_b and neither of them are NAT)
366
+ if left != right and left != NPY_NAT and right != NPY_NAT:
367
+ mismatch[i] = 1
368
+
369
+ trans_idx = mismatch.nonzero()[0 ]
370
+
361
371
if trans_idx.size == 1 :
362
- stamp = _render_tstamp(vals[trans_idx])
372
+ stamp = _render_tstamp(vals[trans_idx[ 0 ] ])
363
373
raise pytz.AmbiguousTimeError(
364
374
f" Cannot infer dst time from {stamp} as there "
365
375
" are no repeated times"
@@ -385,14 +395,14 @@ cdef ndarray[int64_t] _get_dst_hours(
385
395
386
396
# Find the index for the switch and pull from a for dst and b
387
397
# for standard
388
- switch_idx = (delta <= 0 ).nonzero()[0 ]
389
- if switch_idx .size > 1 :
398
+ switch_idxs = (delta <= 0 ).nonzero()[0 ]
399
+ if switch_idxs .size > 1 :
390
400
raise pytz.AmbiguousTimeError(
391
- f" There are {switch_idx .size} dst switches when "
401
+ f" There are {switch_idxs .size} dst switches when "
392
402
" there should only be 1."
393
403
)
394
404
395
- switch_idx = switch_idx [0 ] + 1 # TODO: declare type for switch_idx
405
+ switch_idx = switch_idxs [0 ] + 1
396
406
# Pull the only index and adjust
397
407
a_idx = grp[:switch_idx]
398
408
b_idx = grp[switch_idx:]
0 commit comments