Skip to content

Commit 4482dd8

Browse files
committed
refactor
1 parent 41d096b commit 4482dd8

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

pandas/_libs/tslibs/tzconversion.pyx

+16-20
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ timedelta-like}
237237
Py_ssize_t i, n = vals.shape[0]
238238
Py_ssize_t delta_idx_offset, delta_idx
239239
int64_t v, left, right, val, new_local, remaining_mins
240-
int64_t first_delta, delta
240+
int64_t delta
241241
int64_t shift_delta = 0
242242
ndarray[int64_t] result_a, result_b, dst_hours
243243
int64_t[::1] result
@@ -327,22 +327,6 @@ timedelta-like}
327327
if infer_dst:
328328
dst_hours = _get_dst_hours(vals, result_a, result_b, creso=creso)
329329

330-
# Pre-compute delta_idx_offset that will be used if we go down non-existent
331-
# paths.
332-
# Shift the delta_idx by if the UTC offset of
333-
# the target tz is greater than 0 and we're moving forward
334-
# or vice versa
335-
# TODO: delta_idx_offset and info.deltas are needed for zoneinfo timezones,
336-
# but are not applicable for all timezones. Setting the former to 0 and
337-
# length checking the latter avoids UB, but this could use a larger refactor
338-
delta_idx_offset = 0
339-
if len(info.deltas):
340-
first_delta = info.deltas[0]
341-
if (shift_forward or shift_delta > 0) and first_delta > 0:
342-
delta_idx_offset = 1
343-
elif (shift_backward or shift_delta < 0) and first_delta < 0:
344-
delta_idx_offset = 1
345-
346330
for i in range(n):
347331
val = vals[i]
348332
left = result_a[i]
@@ -416,9 +400,21 @@ timedelta-like}
416400

417401
else:
418402
delta_idx = bisect_right_i8(info.tdata, new_local, info.ntrans)
419-
if (shift_forward or shift_delta > 0) and \
420-
info.deltas[delta_idx-1] >= 0:
421-
delta_idx_offset = 1
403+
404+
# Shift the delta_idx by if the UTC offset of
405+
# the target tz is greater than 0 and we're moving forward
406+
# or vice versa
407+
# TODO: delta_idx_offset and info.deltas are needed for zoneinfo
408+
# timezones, but are not applicable for all timezones. Setting the
409+
# former to 0 and length checking the latter avoids UB, but this
410+
# could use a larger refactor
411+
delta_idx_offset = 0
412+
if len(info.deltas):
413+
delta = info.deltas[delta_idx-1]
414+
if (shift_forward or shift_delta > 0) and delta >= 0:
415+
delta_idx_offset = 1
416+
elif (shift_backward or shift_delta < 0) and delta < 0:
417+
delta_idx_offset = 1
422418

423419
delta_idx = delta_idx - delta_idx_offset
424420
result[i] = new_local - info.deltas[delta_idx]

0 commit comments

Comments
 (0)