From 5d2a7ffce0931d79c49b3020d66edbe3474d667c Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Fri, 27 Oct 2023 08:46:55 -0400 Subject: [PATCH 1/3] fix segfault with tz_localize_to_utc --- pandas/_libs/tslibs/tzconversion.pyx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/tzconversion.pyx b/pandas/_libs/tslibs/tzconversion.pyx index 9c8865fbdf428..44fa438727700 100644 --- a/pandas/_libs/tslibs/tzconversion.pyx +++ b/pandas/_libs/tslibs/tzconversion.pyx @@ -332,7 +332,10 @@ timedelta-like} # Shift the delta_idx by if the UTC offset of # the target tz is greater than 0 and we're moving forward # or vice versa - first_delta = info.deltas[0] + if len(info.deltas): + first_delta = info.deltas[0] + else: + first_delta = 0 if (shift_forward or shift_delta > 0) and first_delta > 0: delta_idx_offset = 1 elif (shift_backward or shift_delta < 0) and first_delta < 0: From d80da2a9a16cd455d8198dee95be02071aee20fa Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Mon, 30 Oct 2023 19:33:09 -0400 Subject: [PATCH 2/3] refactor --- pandas/_libs/tslibs/tzconversion.pyx | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/pandas/_libs/tslibs/tzconversion.pyx b/pandas/_libs/tslibs/tzconversion.pyx index 44fa438727700..5248277dd6772 100644 --- a/pandas/_libs/tslibs/tzconversion.pyx +++ b/pandas/_libs/tslibs/tzconversion.pyx @@ -332,16 +332,13 @@ timedelta-like} # Shift the delta_idx by if the UTC offset of # the target tz is greater than 0 and we're moving forward # or vice versa + delta_idx_offset = 0 if len(info.deltas): first_delta = info.deltas[0] - else: - first_delta = 0 - if (shift_forward or shift_delta > 0) and first_delta > 0: - delta_idx_offset = 1 - elif (shift_backward or shift_delta < 0) and first_delta < 0: - delta_idx_offset = 1 - else: - delta_idx_offset = 0 + if (shift_forward or shift_delta > 0) and first_delta > 0: + delta_idx_offset = 1 + elif (shift_backward or shift_delta < 0) and first_delta < 0: + delta_idx_offset = 1 for i in range(n): val = vals[i] From dc71085493d9eb54443bbcc57444bc07e104afc8 Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Wed, 1 Nov 2023 12:33:11 -0400 Subject: [PATCH 3/3] added TODO --- pandas/_libs/tslibs/tzconversion.pyx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/_libs/tslibs/tzconversion.pyx b/pandas/_libs/tslibs/tzconversion.pyx index 5248277dd6772..e77a385113e93 100644 --- a/pandas/_libs/tslibs/tzconversion.pyx +++ b/pandas/_libs/tslibs/tzconversion.pyx @@ -332,6 +332,9 @@ timedelta-like} # Shift the delta_idx by if the UTC offset of # the target tz is greater than 0 and we're moving forward # or vice versa + # TODO: delta_idx_offset and info.deltas are needed for zoneinfo timezones, + # but are not applicable for all timezones. Setting the former to 0 and + # length checking the latter avoids UB, but this could use a larger refactor delta_idx_offset = 0 if len(info.deltas): first_delta = info.deltas[0]