question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: round() on tz-aware DateTimeIndex (spanning DST switch) throws AmbiguousTimeError as if tz-unaware

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd
dt = pd.Timestamp("2020-10-25 02:00:00").tz_localize("Europe/Brussels", ambiguous=False).round('min')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/_libs/tslibs/timestamps.pyx", line 489, in pandas._libs.tslibs.timestamps.Timestamp.round
  File "pandas/_libs/tslibs/timestamps.pyx", line 443, in pandas._libs.tslibs.timestamps.Timestamp._round
  File "pandas/_libs/tslibs/timestamps.pyx", line 826, in pandas._libs.tslibs.timestamps.Timestamp.tz_localize
  File "pandas/_libs/tslibs/tzconversion.pyx", line 232, in pandas._libs.tslibs.tzconversion.tz_localize_to_utc
pytz.exceptions.AmbiguousTimeError: Cannot infer dst time from 2020-10-25 02:00:00, try using the 'ambiguous' argument
import pandas as pd
dt = pd.Timestamp("2020-10-25 02:00:00").tz_localize("Europe/Brussels", ambiguous=True).round('min')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/_libs/tslibs/timestamps.pyx", line 489, in pandas._libs.tslibs.timestamps.Timestamp.round
  File "pandas/_libs/tslibs/timestamps.pyx", line 443, in pandas._libs.tslibs.timestamps.Timestamp._round
  File "pandas/_libs/tslibs/timestamps.pyx", line 826, in pandas._libs.tslibs.timestamps.Timestamp.tz_localize
  File "pandas/_libs/tslibs/tzconversion.pyx", line 232, in pandas._libs.tslibs.tzconversion.tz_localize_to_utc
pytz.exceptions.AmbiguousTimeError: Cannot infer dst time from 2020-10-25 02:00:00, try using the 'ambiguous' argument

Problem description

A localized Timestamp does not give an error when rounding. A Series of a localized DateTimeIndex shouldn’t either.

[this should explain why the current behaviour is a problem and why the expected output is a better solution]

Expected Output

Output is expected to be identical as for

pandas.Timestamp("2020-10-25 02:00:00+01:00").round('min')

respectively

pandas.Timestamp("2020-10-25 02:00:0+02:00").round('min')

i.e. without AmbiguousTimeError

Output of pd.show_versions()

[paste the output of pd.show_versions() here leaving a blank line after the details tag]

INSTALLED VERSIONS

commit : db08276bc116c438d3fdee492026f8223584c477 python : 3.8.5.final.0 python-bits : 64 OS : Linux OS-release : 5.9.1-050901-generic Version : #202010170731 SMP Sat Oct 17 07:42:59 UTC 2020 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 1.1.3 numpy : 1.19.3 pytz : 2020.1 dateutil : 2.8.1 pip : 20.0.2 setuptools : 44.0.0

rest is not installed

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mroeschkecommented, Nov 13, 2021

Yes, and the documentation will be updated in https://github.com/pandas-dev/pandas/pull/44357 (available in version 1.4) to reflect this. Closing.

1reaction
bluppfiskcommented, Oct 29, 2020

Note that a workaround exists by converting to UTC first:

>>> pd.Timestamp("2020-10-25 02:00:00").tz_localize("Europe/Brussels", ambiguous=True).astimezone("UTC").round('min').astimezone("Europe/Brussels")
Read more comments on GitHub >

github_iconTop Results From Across the Web

pandas.DatetimeIndex.tz_localize
Array/Index converted to the specified time zone. Raises. TypeError. If the Datetime Array/Index is tz-aware and tz is not None.
Read more >
dataframe.Series.dt. - round - Dask documentation
Perform round operation on the data to the specified freq . ... 'raise' will raise an AmbiguousTimeError if there are ambiguous times.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found