Skip to content

BUG: Timestamp(ambiguous, tz=from_pytz) not raising #55712

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 3 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ Timedelta
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`)
-

Numeric
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ cdef datetime _localize_pydatetime(datetime dt, tzinfo tz):
"""
try:
# datetime.replace with pytz may be incorrect result
return tz.localize(dt)
return tz.localize(dt, is_dst=None)
Copy link
Member

Choose a reason for hiding this comment

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

Should we have a TODO comment here noting that we may want to respect fold here in the future?

Copy link
Member Author

Choose a reason for hiding this comment

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

good idea, updated

except AttributeError:
return dt.replace(tzinfo=tz)

Expand Down
30 changes: 30 additions & 0 deletions pandas/tests/indexes/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,36 @@ def test_dti_convert_datetime_list(self, tzstr):
dr2 = DatetimeIndex(list(dr), name="foo", freq="D")
tm.assert_index_equal(dr, dr2)

def test_dti_ambiguous_matches_timestamp(self):
# GH#47471 check that we get the same raising behavior in the DTI
# constructor and Timestamp constructor
dtstr = "2013-11-03 01:59:59.999999"
dtobj = Timestamp(dtstr).to_pydatetime()

tz = pytz.timezone("US/Eastern")
with pytest.raises(pytz.AmbiguousTimeError, match=dtstr):
Timestamp(dtstr, tz=tz)
with pytest.raises(pytz.AmbiguousTimeError, match=dtstr):
Timestamp(dtobj, tz=tz)
with pytest.raises(pytz.AmbiguousTimeError, match=dtstr):
DatetimeIndex([dtstr], tz=tz)
with pytest.raises(pytz.AmbiguousTimeError, match=dtstr):
DatetimeIndex([dtobj], tz=tz)

tz2 = gettz("US/Eastern")
with pytest.raises(pytz.AmbiguousTimeError, match=dtstr):
Timestamp(dtstr, tz=tz2)
# FIXME: The Timestamp constructor here behaves differently than all
# the other cases bc with dateutil/zoneinfo tzinfos we implicitly
# get fold=0. Having this raise is not important, but having the
# behavior be consistent across cases is.
# with pytest.raises(pytz.AmbiguousTimeError, match=dtstr):
# Timestamp(dtobj, tz=tz2)
with pytest.raises(pytz.AmbiguousTimeError, match=dtstr):
DatetimeIndex([dtstr], tz=tz2)
with pytest.raises(pytz.AmbiguousTimeError, match=dtstr):
DatetimeIndex([dtobj], tz=tz2)


class TestTimeSeries:
def test_dti_constructor_preserve_dti_freq(self):
Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/tseries/offsets/test_dst.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
YearBegin,
YearEnd,
)
from pandas.errors import PerformanceWarning

from pandas import DatetimeIndex
import pandas._testing as tm
from pandas.util.version import Version

# error: Module has no attribute "__version__"
Expand Down Expand Up @@ -83,6 +86,29 @@ def _test_all_offsets(self, n, **kwds):
def _test_offset(self, offset_name, offset_n, tstart, expected_utc_offset):
offset = DateOffset(**{offset_name: offset_n})

if (
offset_name in ["hour", "minute", "second", "microsecond"]
and offset_n == 1
and tstart == Timestamp("2013-11-03 01:59:59.999999-0500", tz="US/Eastern")
):
# This addition results in an ambiguous wall time
err_msg = {
"hour": "2013-11-03 01:59:59.999999",
"minute": "2013-11-03 01:01:59.999999",
"second": "2013-11-03 01:59:01.999999",
"microsecond": "2013-11-03 01:59:59.000001",
}[offset_name]
with pytest.raises(pytz.AmbiguousTimeError, match=err_msg):
tstart + offset
# While we're here, let's check that we get the same behavior in a
# vectorized path
dti = DatetimeIndex([tstart])
warn_msg = "Non-vectorized DateOffset"
with pytest.raises(pytz.AmbiguousTimeError, match=err_msg):
with tm.assert_produces_warning(PerformanceWarning, match=warn_msg):
dti + offset
return

t = tstart + offset
if expected_utc_offset is not None:
assert get_utc_offset_hours(t) == expected_utc_offset
Expand Down