Skip to content

Commit

Permalink
Merge branch 'list_accessor' of https://github.com/rohanjain101/pandas
Browse files Browse the repository at this point in the history
…into list_accessor
  • Loading branch information
Rohan Jain committed Nov 1, 2023
2 parents 79962be + 4b57189 commit dc17c0f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
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 @@ -362,6 +362,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
3 changes: 2 additions & 1 deletion pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,8 @@ cdef datetime _localize_pydatetime(datetime dt, tzinfo tz):
"""
try:
# datetime.replace with pytz may be incorrect result
return tz.localize(dt)
# TODO: try to respect `fold` attribute
return tz.localize(dt, is_dst=None)
except AttributeError:
return dt.replace(tzinfo=tz)

Expand Down
5 changes: 4 additions & 1 deletion pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,10 @@ def __call__(self, *args, **kwargs):
return plot_backend.plot(self._parent, x=x, y=y, kind=kind, **kwargs)

if kind not in self._all_kinds:
raise ValueError(f"{kind} is not a valid plot kind")
raise ValueError(
f"{kind} is not a valid plot kind "
f"Valid plot kinds: {self._all_kinds}"
)

# The original data structured can be transformed before passed to the
# backend. For example, for DataFrame is common to set the index as the
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)

@pytest.mark.parametrize("tz", [None, "UTC", "US/Pacific"])
def test_dti_constructor_with_non_nano_dtype(self, tz):
# GH#55756, GH#54620
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

0 comments on commit dc17c0f

Please sign in to comment.