Skip to content

Commit 8113813

Browse files
Backport PR #45087: BUG: Timestamp.to_pydatetime losing 'fold' (#45330)
Co-authored-by: jbrockmendel <[email protected]>
1 parent 416e622 commit 8113813

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ Datetimelike
724724
- ``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`)
725725
- Bug in adding a ``np.timedelta64`` object to a :class:`BusinessDay` or :class:`CustomBusinessDay` object incorrectly raising (:issue:`44532`)
726726
- 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`)
727+
- Bug in :meth:`Timestamp.to_pydatetime` failing to retain the ``fold`` attribute (:issue:`45087`)
727728
- Bug in :meth:`Series.mode` with ``DatetimeTZDtype`` incorrectly returning timezone-naive and ``PeriodDtype`` incorrectly raising (:issue:`41927`)
728729
- Bug in :class:`DateOffset`` addition with :class:`Timestamp` where ``offset.nanoseconds`` would not be included in the result (:issue:`43968`, :issue:`36589`)
729730
- Bug in :meth:`Timestamp.fromtimestamp` not supporting the ``tz`` argument (:issue:`45083`)

pandas/_libs/tslibs/timestamps.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ cdef class _Timestamp(ABCTimestamp):
898898

899899
return datetime(self.year, self.month, self.day,
900900
self.hour, self.minute, self.second,
901-
self.microsecond, self.tzinfo)
901+
self.microsecond, self.tzinfo, fold=self.fold)
902902

903903
cpdef to_datetime64(self):
904904
"""

pandas/tests/scalar/timestamp/test_timestamp.py

+7
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,13 @@ def test_conversion(self):
592592
assert type(result) == type(expected)
593593
assert result.dtype == expected.dtype
594594

595+
def test_to_pydatetime_fold(self):
596+
# GH#45087
597+
tzstr = "dateutil/usr/share/zoneinfo/America/Chicago"
598+
ts = Timestamp(year=2013, month=11, day=3, hour=1, minute=0, fold=1, tz=tzstr)
599+
dt = ts.to_pydatetime()
600+
assert dt.fold == 1
601+
595602
def test_to_pydatetime_nonzero_nano(self):
596603
ts = Timestamp("2011-01-01 9:00:00.123456789")
597604

pandas/tests/scalar/timestamp/test_timezones.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ def test_tz_localize_ambiguous_compat(self):
166166
assert result_pytz.value == 1382835600000000000
167167

168168
# fixed ambiguous behavior
169-
# see gh-14621
169+
# see gh-14621, GH#45087
170170
assert result_pytz.to_pydatetime().tzname() == "GMT"
171-
assert result_dateutil.to_pydatetime().tzname() == "BST"
171+
assert result_dateutil.to_pydatetime().tzname() == "GMT"
172172
assert str(result_pytz) == str(result_dateutil)
173173

174174
# 1 hour difference

0 commit comments

Comments
 (0)