Skip to content

BUG: DatetimeIndex.floor works, but .ceil does not, during a DST-changeover #49567

Open
@rwijtvliet

Description

@rwijtvliet

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
i = pd.date_range("2020-03-28 23:50", periods=5, freq='H', tz ="Europe/Berlin")

# This works:
i.floor('H')
# DatetimeIndex(['2020-03-28 23:00:00+01:00', '2020-03-29 00:00:00+01:00',
#               '2020-03-29 01:00:00+01:00', '2020-03-29 03:00:00+02:00',
#               '2020-03-29 04:00:00+02:00'],
#              dtype='datetime64[ns, Europe/Berlin]', freq=None)

# But this does not:
i.ceil('H') # NonExistentTimeError: 2020-03-29 02:00:00

Issue Description

.ceil seems to try to round up to the next full "wall" hour, without considering a possible change to the UTC-offset.

Expected Behavior

In this case, the expected outcome is the same as

i.floor('H') + pd.Timedelta(hours=1)
# DatetimeIndex(['2020-03-29 00:00:00+01:00', '2020-03-29 01:00:00+01:00',
#               '2020-03-29 03:00:00+02:00', '2020-03-29 04:00:00+02:00',
#               '2020-03-29 05:00:00+02:00'],
#              dtype='datetime64[ns, Europe/Berlin]', freq=None)

However, this is not a general workaround: it requires that all timestamps in i do not correspond to a full hour.

Installed Versions

INSTALLED VERSIONS

commit : 91111fd
python : 3.9.13.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 165 Stepping 2, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : de_DE.cp1252

pandas : 1.5.1
numpy : 1.23.4
pytz : 2022.6
dateutil : 2.8.2
setuptools : 63.4.1
pip : 22.2.2
Cython : None
pytest : 7.1.3
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.5.0
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions