Skip to content

Timestamp.replace should handle ambiguous DST times gracefully #25017

Closed
@ali-tny

Description

@ali-tny
>>> tz = pytz.timezone('Europe/London')
>>> d = datetime.datetime(2019, 10, 27, 1, 0)
>>> d = tz.localize(d, is_dst=True)
>>> ts = pd.Timestamp(d)
>>> ts == d 
True
>>> pd.Timestamp(d).replace(minute=0) == d.replace(minute=0)
False
>>> pd.Timestamp(d).replace(minute=0).tzinfo
<DstTzInfo 'Europe/London' GMT0:00:00 STD>
>>> d.replace(minute=0).tzinfo
<DstTzInfo 'Europe/London' BST+1:00:00 DST>

This is related to issue 18319 and the associated PR which makes pandas.Timestamp.replace behaviour deviate from datetime.datetime.replace when crossing DST transitions, and generally hand them more gracefully.
In cases when replacing into an ambiguous time (as above, when the clocks go back and repeat the same time in local time), the default behaviour of pandas is to normalize the datetime, using pytz's default setting is_dst=False. It would be useful to

  • have a way of specifying whether to go into DST or not and override the pytz default
  • in the case of replacing increments less than an hour, to copy the DST info of the original datetime (since the new time could be assumed to be in the same local hour)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions