Skip to content

BUG: Bad shift_forward around UTC+0 when DST #56017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ Timezones
^^^^^^^^^
- Bug in :class:`AbstractHolidayCalendar` where timezone data was not propagated when computing holiday observances (:issue:`54580`)
- Bug in :class:`Timestamp` construction with an ambiguous value and a ``pytz`` timezone failing to raise ``pytz.AmbiguousTimeError`` (:issue:`55657`)
-
- Bug in :meth:`Timestamp.tz_localize` with ``nonexistent="shift_forward`` around UTC+0 during DST (:issue:`51501`)

Numeric
^^^^^^^
Expand Down
3 changes: 3 additions & 0 deletions pandas/_libs/tslibs/tzconversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,9 @@ timedelta-like}

else:
delta_idx = bisect_right_i8(info.tdata, new_local, info.ntrans)
if (shift_forward or shift_delta > 0) and \
info.deltas[delta_idx-1] >= 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
info.deltas[delta_idx-1] >= 0:
info.deltas[delta_idx - 1] >= 0:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

delta_idx_offset = 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's safe to permanently change this to 1 for future iterations.

Might be safer to do something like

dst_offset = 0 if condition else 1
delta_idx = delta_idx - min(delta_idx_offset + dst_offset, 1)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, make sense.

But there is this piece of code just before the loop to precompute delta_idx_offset. (Search for "Pre-compute delta_idx_offset"). I moved the whole block to here.

I don't know why it was precomputed. Was we assuming that there are just one time zone in the array?

Further more there is a recent PR around that block of code for a segfault problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh misunderstood what you mean. Corrected.


delta_idx = delta_idx - delta_idx_offset
result[i] = new_local - info.deltas[delta_idx]
Expand Down
38 changes: 38 additions & 0 deletions pandas/tests/scalar/timestamp/methods/test_tz_localize.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,44 @@ def test_tz_localize_nonexistent(self, stamp, tz):
ts.tz_localize(tz, nonexistent="raise")
assert ts.tz_localize(tz, nonexistent="NaT") is NaT

@pytest.mark.parametrize(
"stamp, tz, forward_expected, backward_expected",
[
(
"2015-03-29 02:00:00",
"Europe/Warsaw",
"2015-03-29 03:00:00",
"2015-03-29 01:59:59",
), # utc+1 -> utc+2
(
"2023-03-12 02:00:00",
"America/Los_Angeles",
"2023-03-12 03:00:00",
"2023-03-12 01:59:59",
), # utc-8 -> utc-7
(
"2023-03-26 01:00:00",
"Europe/London",
"2023-03-26 02:00:00",
"2023-03-26 00:59:59",
), # utc+0 -> utc+1
(
"2023-03-26 00:00:00",
"Atlantic/Azores",
"2023-03-26 01:00:00",
"2023-03-25 23:59:59",
), # utc-1 -> utc+0
],
)
def test_tz_localize_nonexistent_shift(
self, stamp, tz, forward_expected, backward_expected
):
ts = Timestamp(stamp)
forward_ts = ts.tz_localize(tz, nonexistent="shift_forward")
assert forward_ts == Timestamp(forward_expected, tz=tz)
backward_ts = ts.tz_localize(tz, nonexistent="shift_backward")
assert backward_ts == Timestamp(backward_expected, tz=tz)

def test_tz_localize_ambiguous_raise(self):
# GH#13057
ts = Timestamp("2015-11-1 01:00")
Expand Down