Skip to content

Commit 84bee25

Browse files
authored
BUG: preserve fold in Timestamp.replace (#37644)
1 parent 6ee20b0 commit 84bee25

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ Timezones
412412
^^^^^^^^^
413413

414414
- Bug in :func:`date_range` was raising AmbiguousTimeError for valid input with ``ambiguous=False`` (:issue:`35297`)
415-
-
415+
- Bug in :meth:`Timestamp.replace` was losing fold information (:issue:`37610`)
416416

417417

418418
Numeric

pandas/_libs/tslibs/nattype.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ default 'raise'
774774
microsecond : int, optional
775775
nanosecond : int, optional
776776
tzinfo : tz-convertible, optional
777-
fold : int, optional, default is 0
777+
fold : int, optional
778778
779779
Returns
780780
-------

pandas/_libs/tslibs/timestamps.pyx

+7-2
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,7 @@ default 'raise'
13741374
microsecond=None,
13751375
nanosecond=None,
13761376
tzinfo=object,
1377-
fold=0,
1377+
fold=None,
13781378
):
13791379
"""
13801380
implements datetime.replace, handles nanoseconds.
@@ -1390,7 +1390,7 @@ default 'raise'
13901390
microsecond : int, optional
13911391
nanosecond : int, optional
13921392
tzinfo : tz-convertible, optional
1393-
fold : int, optional, default is 0
1393+
fold : int, optional
13941394
13951395
Returns
13961396
-------
@@ -1407,6 +1407,11 @@ default 'raise'
14071407
# set to naive if needed
14081408
tzobj = self.tzinfo
14091409
value = self.value
1410+
1411+
# GH 37610. Preserve fold when replacing.
1412+
if fold is None:
1413+
fold = self.fold
1414+
14101415
if tzobj is not None:
14111416
value = tz_convert_from_utc_single(value, tzobj)
14121417

pandas/tests/scalar/timestamp/test_unary_ops.py

+11
Original file line numberDiff line numberDiff line change
@@ -424,3 +424,14 @@ def test_timestamp(self):
424424
# should agree with datetime.timestamp method
425425
dt = ts.to_pydatetime()
426426
assert dt.timestamp() == ts.timestamp()
427+
428+
429+
@pytest.mark.parametrize("fold", [0, 1])
430+
def test_replace_preserves_fold(fold):
431+
# GH 37610. Check that replace preserves Timestamp fold property
432+
tz = gettz("Europe/Moscow")
433+
434+
ts = Timestamp(year=2009, month=10, day=25, hour=2, minute=30, fold=fold, tzinfo=tz)
435+
ts_replaced = ts.replace(second=1)
436+
437+
assert ts_replaced.fold == fold

0 commit comments

Comments
 (0)