From 4f99c32b17f4b89383991e4702262b4ea3e3813d Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 12 Jan 2022 05:49:06 -0800 Subject: [PATCH] BUG: Timestamp.to_pydatetime losing 'fold' (#45087) --- doc/source/whatsnew/v1.4.0.rst | 1 + pandas/_libs/tslibs/timestamps.pyx | 2 +- pandas/tests/scalar/timestamp/test_timestamp.py | 7 +++++++ pandas/tests/scalar/timestamp/test_timezones.py | 4 ++-- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 5dd6fa0d4f72d..ac638e6bd3c6e 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -724,6 +724,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:`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`) - Bug in :meth:`Timestamp.fromtimestamp` not supporting the ``tz`` argument (:issue:`45083`) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index b13b77506ecc7..92a00d682a7e5 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -898,7 +898,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 f734c331e3def..5f7cca99f75c6 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -592,6 +592,13 @@ def test_conversion(self): assert type(result) == type(expected) 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() + assert dt.fold == 1 + def test_to_pydatetime_nonzero_nano(self): ts = Timestamp("2011-01-01 9:00:00.123456789") 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