Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ Timezones
^^^^^^^^^

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


Numeric
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/nattype.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ default 'raise'
microsecond : int, optional
nanosecond : int, optional
tzinfo : tz-convertible, optional
fold : int, optional, default is 0
fold : int, optional

Returns
-------
Expand Down
9 changes: 7 additions & 2 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,7 @@ default 'raise'
microsecond=None,
nanosecond=None,
tzinfo=object,
fold=0,
fold=None,
):
"""
implements datetime.replace, handles nanoseconds.
Expand All @@ -1390,7 +1390,7 @@ default 'raise'
microsecond : int, optional
nanosecond : int, optional
tzinfo : tz-convertible, optional
fold : int, optional, default is 0
fold : int, optional

Returns
-------
Expand All @@ -1407,6 +1407,11 @@ default 'raise'
# set to naive if needed
tzobj = self.tzinfo
value = self.value

# GH 37610. Preserve fold when replacing.
if fold is None:
fold = self.fold

if tzobj is not None:
value = tz_convert_from_utc_single(value, tzobj)

Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/indexes/datetimes/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,3 +1190,13 @@ def test_tz_localize_invalidates_freq():
dti2 = dti[:1]
result = dti2.tz_localize("US/Eastern")
assert result.freq == "H"


def test_replace_preserves_fold():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should go here: pandas/tests/scalar/timestamp/test_unary_ops.py (there is another replace test)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

# GH 37610. Check that replace preserves Timestamp fold property
tz = gettz("Europe/Moscow")

result = Timestamp(1256427000000000000, tz=tz, unit="ns").replace(tzinfo=tz).fold
expected = 1

assert result == expected