Skip to content

ENH: handle nonexistent shift for ZoneInfo #50219

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 4 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 20 additions & 6 deletions pandas/_libs/tslibs/tzconversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ timedelta-like}
str stamp
Localizer info = Localizer(tz, creso=creso)
int64_t pph = periods_per_day(creso) // 24
int64_t pps = periods_per_second(creso)
npy_datetimestruct dts

# Vectorized version of DstTzInfo.localize
if info.use_utc:
Expand Down Expand Up @@ -388,14 +390,26 @@ timedelta-like}
new_local = val - remaining_mins - 1

if is_zi:
raise NotImplementedError(
"nonexistent shifting is not implemented with ZoneInfo tzinfos"
)
# use the same construction as in _get_utc_bounds_zoneinfo
pandas_datetime_to_datetimestruct(new_local, creso, &dts)
extra = (dts.ps // 1000) * (pps // 1_000_000_000)

dt = datetime_new(dts.year, dts.month, dts.day, dts.hour,
dts.min, dts.sec, dts.us, None)

delta_idx = bisect_right_i8(info.tdata, new_local, info.ntrans)
if shift_forward or shift_delta > 0:
dt = dt.replace(tzinfo=tz, fold=1)
else:
dt = dt.replace(tzinfo=tz, fold=0)
dt = dt.astimezone(utc_stdlib)
dt = dt.replace(tzinfo=None)
result[i] = pydatetime_to_dt64(dt, &dts, creso) + extra

else:
delta_idx = bisect_right_i8(info.tdata, new_local, info.ntrans)

delta_idx = delta_idx - delta_idx_offset
result[i] = new_local - info.deltas[delta_idx]
delta_idx = delta_idx - delta_idx_offset
result[i] = new_local - info.deltas[delta_idx]
elif fill_nonexist:
result[i] = NPY_NAT
else:
Expand Down
9 changes: 1 addition & 8 deletions pandas/tests/indexes/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,15 +880,8 @@ def test_constructor_with_ambiguous_keyword_arg(self):
result = date_range(end=end, periods=2, ambiguous=False)
tm.assert_index_equal(result, expected)

def test_constructor_with_nonexistent_keyword_arg(self, warsaw, request):
def test_constructor_with_nonexistent_keyword_arg(self, warsaw):
# GH 35297
if type(warsaw).__name__ == "ZoneInfo":
mark = pytest.mark.xfail(
reason="nonexistent-shift not yet implemented for ZoneInfo",
raises=NotImplementedError,
)
request.node.add_marker(mark)

timezone = warsaw

# nonexistent keyword in start
Expand Down
10 changes: 1 addition & 9 deletions pandas/tests/series/methods/test_tz_localize.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def test_series_tz_localize_matching_index(self):
"method, exp",
[
["shift_forward", "2015-03-29 03:00:00"],
["shift_backward", "2015-03-29 01:59:59.999999999"],
["NaT", NaT],
["raise", None],
["foo", "invalid"],
Expand Down Expand Up @@ -99,15 +100,6 @@ def test_tz_localize_nonexistent(self, warsaw, method, exp):
with pytest.raises(ValueError, match=msg):
df.tz_localize(tz, nonexistent=method)

elif method == "shift_forward" and type(tz).__name__ == "ZoneInfo":
msg = "nonexistent shifting is not implemented with ZoneInfo tzinfos"
with pytest.raises(NotImplementedError, match=msg):
ser.tz_localize(tz, nonexistent=method)
with pytest.raises(NotImplementedError, match=msg):
df.tz_localize(tz, nonexistent=method)
with pytest.raises(NotImplementedError, match=msg):
dti.tz_localize(tz, nonexistent=method)

else:
result = ser.tz_localize(tz, nonexistent=method)
expected = Series(1, index=DatetimeIndex([exp] * n, tz=tz))
Expand Down