From f42676d19bbc937ea348a645605a0ccc2200707f Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 27 Dec 2021 21:12:02 -0800 Subject: [PATCH 1/3] BUG: Timestamp.to_pydatetime losing 'fold' --- doc/source/whatsnew/v1.4.0.rst | 1 + pandas/_libs/tslibs/timestamps.pyx | 2 +- pandas/tests/scalar/timestamp/test_timestamp.py | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index c743e38a118f7..96bd5db2a3c79 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -680,6 +680,7 @@ Datetimelike - ``np.maximum.reduce`` and ``np.minimum.reduce`` now correctly return :class:`Timestamp` and :class:`Timedelta` objects when operating on :class:`Series`, :class:`DataFrame`, or :class:`Index` with ``datetime64[ns]`` or ``timedelta64[ns]`` dtype (:issue:`43923`) - Bug in adding a ``np.timedelta64`` object to a :class:`BusinessDay` or :class:`CustomBusinessDay` object incorrectly raising (:issue:`44532`) - Bug in :meth:`Index.insert` for inserting ``np.datetime64``, ``np.timedelta64`` or ``tuple`` into :class:`Index` with ``dtype='object'`` with negative loc adding ``None`` and replacing existing value (:issue:`44509`) +- Bug in :meth:`Timestamp.to_pydatetime` failing to retain the ``fold`` attribute (:issue:`??`) - Bug in :meth:`Series.mode` with ``DatetimeTZDtype`` incorrectly returning timezone-naive and ``PeriodDtype`` incorrectly raising (:issue:`41927`) - Bug in :class:`DateOffset`` addition with :class:`Timestamp` where ``offset.nanoseconds`` would not be included in the result (:issue:`43968`, :issue:`36589`) - diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 1c26793876e5a..0a286078628d0 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -896,7 +896,7 @@ cdef class _Timestamp(ABCTimestamp): return datetime(self.year, self.month, self.day, self.hour, self.minute, self.second, - self.microsecond, self.tzinfo) + self.microsecond, self.tzinfo, fold=self.fold) cpdef to_datetime64(self): """ diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index 9c9e11c6a4ba4..402df39e95dff 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -552,6 +552,12 @@ def test_conversion(self): assert type(result) == type(expected) assert result.dtype == expected.dtype + def test_to_pydatetime_fold(self): + tzstr = "dateutil/usr/share/zoneinfo/America/Chicago" + ts = Timestamp(year=2013, month=11, day=3, hour=1, minute=0, fold=1, tz=tzstr) + dt = ts.to_pydatetime() + assert dt.fold == 1 + def test_to_pydatetime_nonzero_nano(self): ts = Timestamp("2011-01-01 9:00:00.123456789") From 32c0d53b31845bceb9856ebefcd2332621963b81 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 27 Dec 2021 21:13:37 -0800 Subject: [PATCH 2/3] GH ref --- doc/source/whatsnew/v1.4.0.rst | 2 +- pandas/tests/scalar/timestamp/test_timestamp.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 96bd5db2a3c79..b9fcc7bfeec99 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -680,7 +680,7 @@ Datetimelike - ``np.maximum.reduce`` and ``np.minimum.reduce`` now correctly return :class:`Timestamp` and :class:`Timedelta` objects when operating on :class:`Series`, :class:`DataFrame`, or :class:`Index` with ``datetime64[ns]`` or ``timedelta64[ns]`` dtype (:issue:`43923`) - Bug in adding a ``np.timedelta64`` object to a :class:`BusinessDay` or :class:`CustomBusinessDay` object incorrectly raising (:issue:`44532`) - Bug in :meth:`Index.insert` for inserting ``np.datetime64``, ``np.timedelta64`` or ``tuple`` into :class:`Index` with ``dtype='object'`` with negative loc adding ``None`` and replacing existing value (:issue:`44509`) -- Bug in :meth:`Timestamp.to_pydatetime` failing to retain the ``fold`` attribute (:issue:`??`) +- Bug in :meth:`Timestamp.to_pydatetime` failing to retain the ``fold`` attribute (:issue:`45087`) - Bug in :meth:`Series.mode` with ``DatetimeTZDtype`` incorrectly returning timezone-naive and ``PeriodDtype`` incorrectly raising (:issue:`41927`) - Bug in :class:`DateOffset`` addition with :class:`Timestamp` where ``offset.nanoseconds`` would not be included in the result (:issue:`43968`, :issue:`36589`) - diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index 402df39e95dff..ad5dffde2535b 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -553,6 +553,7 @@ def test_conversion(self): assert result.dtype == expected.dtype def test_to_pydatetime_fold(self): + # GH#45087 tzstr = "dateutil/usr/share/zoneinfo/America/Chicago" ts = Timestamp(year=2013, month=11, day=3, hour=1, minute=0, fold=1, tz=tzstr) dt = ts.to_pydatetime() From 7c352542a2b52f228a6864004bf50fbacd307651 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 11 Jan 2022 12:41:11 -0800 Subject: [PATCH 3/3] fix test per pgannsle comment --- pandas/tests/scalar/timestamp/test_timezones.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/scalar/timestamp/test_timezones.py b/pandas/tests/scalar/timestamp/test_timezones.py index 9ba4a2c1f77cd..a7f7393fb3263 100644 --- a/pandas/tests/scalar/timestamp/test_timezones.py +++ b/pandas/tests/scalar/timestamp/test_timezones.py @@ -166,9 +166,9 @@ def test_tz_localize_ambiguous_compat(self): assert result_pytz.value == 1382835600000000000 # fixed ambiguous behavior - # see gh-14621 + # see gh-14621, GH#45087 assert result_pytz.to_pydatetime().tzname() == "GMT" - assert result_dateutil.to_pydatetime().tzname() == "BST" + assert result_dateutil.to_pydatetime().tzname() == "GMT" assert str(result_pytz) == str(result_dateutil) # 1 hour difference