Skip to content

BUG: Incorrect result for Timestamp.ceil during clock change #49199

Open
@torfsen

Description

@torfsen

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
pd.Timestamp("2022-10-30 02:30 +02:00", tz="Europe/Berlin").ceil("1H").tz_convert("UTC")
# returns Timestamp('2022-10-30 02:00:00+0000', tz='UTC')

Issue Description

The Europe/Berlin timezone has a clock change on 2022-10-30, with the clock moving backwards to 02:00 when reaching 03:00 for the first time (in local time). Compared to UTC, the clock goes:

UTC Europe/Berlin
2022-10-29 23:59 2022-10-30 01:59
2022-10-30 00:00 2022-10-30 02:00
2022-10-30 00:59 2022-10-30 02:59
2022-10-30 01:00 2022-10-30 02:00
2022-10-30 01:59 2022-10-30 02:59
2022-10-30 02:00 2022-10-30 03:00

2022-10-30 02:30 +02 (i.e. 2022-10-30 00:30 +00) is during the "first go", i.e. before the clock jumps back (the same local time in the "second go" is 2022-10-30 02:30 +01, i.e. 2022-10-30 01:30 +00). The next full hour is the time when the clock jumps back, hence I expect

pd.Timestamp("2022-10-30 02:30 +02:00", tz="Europe/Berlin").ceil("1H").tz_convert("UTC")

to return 2022-10-30 01:00 +00. Instead, it returns 2022-10-30 02:00 +00.

This breaks the intuitive assumption that the time delta between the input timestamp and the ceiled timestamp is less than the rounding frequency/delta:

pd.Timestamp("2022-10-30 02:30 +02:00", tz="Europe/Berlin").ceil("1H") - pd.Timestamp("2022-10-30 02:30 +02:00", tz="Europe/Berlin")
# returns  Timedelta('0 days 01:30:00')

This might be related to this comment in #23521 in the sense that Pandas seems to ignore that the timezone is Europe/Berlin and hence carries information about clock changes (as opposed to, say, pytz.FixedOffset(120)).

Expected Behavior

I would have expected ceil to return the next logical hour, upholding the assumption that the time delta between the input timestamp and the ceiled timestamp is less than the rounding frequency/delta. At the very least, I would have expected an exception instead of silently returning an incorrect value.

Installed Versions

INSTALLED VERSIONS

commit : 91111fd
python : 3.8.9.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-128-generic
Version : #144-Ubuntu SMP Tue Sep 20 11:00:04 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.1
numpy : 1.23.4
pytz : 2022.5
dateutil : 2.8.2
setuptools : 63.1.0
pip : 22.2.2
Cython : None
pytest : 7.1.2
hypothesis : None
sphinx : 4.5.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.3
jinja2 : 3.1.2
IPython : 8.3.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : 2.8.1
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 7.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : 3.7.0
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugTimestamppd.Timestamp and associated methodsTimezonesTimezone data dtype

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions