Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 of 3 tasks
bluppfisk opened this issue Oct 29, 2020 · 7 comments
Labels
Bug Frequency DateOffsets Timezones Timezone data dtype

Comments

@bluppfisk
Copy link

  • 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 : db08276
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

@bluppfisk bluppfisk added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 29, 2020
@bluppfisk
Copy link
Author

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")

@jbrockmendel
Copy link
Member

@mroeschke enough of these are building up that I think we can make the Day -> DayDST switch and call it a bugfix instead of an API change. what do you think?

@mroeschke
Copy link
Member

100% in support for 'D' and offsets.Day to mean calendar day xref #22864.

I think it would affect several of operations though (IIRC resample has some sticky logic around 'D'), so I agree it's a "bug fix" in operations like this but an "API change" in others.

@mroeschke mroeschke added Timezones Timezone data dtype Frequency DateOffsets and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 14, 2021
@tnielens
Copy link

Same issue with ceil and floor:

import pandas as pd
pd.Timestamp('2021-10-31 01:30:12+02', tzinfo=dateutil.tz.gettz('Europe/Brussels')).ceil('H')
AmbiguousTimeError                        Traceback (most recent call last)
/tmp/ipykernel_67812/2238318785.py in <module>
----> 1 pd.Timestamp('2021-10-31 01:30:12+02', tzinfo=dateutil.tz.gettz('Europe/Brussels')).ceil('H')
pandas/_libs/tslibs/timestamps.pyx in pandas._libs.tslibs.timestamps.Timestamp.ceil()
pandas/_libs/tslibs/timestamps.pyx in pandas._libs.tslibs.timestamps.Timestamp._round()
pandas/_libs/tslibs/timestamps.pyx in pandas._libs.tslibs.timestamps.Timestamp.tz_localize()
pandas/_libs/tslibs/tzconversion.pyx in pandas._libs.tslibs.tzconversion.tz_localize_to_utc_single()
pandas/_libs/tslibs/tzconversion.pyx in pandas._libs.tslibs.tzconversion.tz_localize_to_utc()
AmbiguousTimeError: Cannot infer dst time from 2021-10-31 02:00:00, try using the 'ambiguous' argument

@jreback
Copy link
Contributor

jreback commented Nov 13, 2021

see #44287

this is as expected

@mroeschke
Copy link
Member

Yes, and the documentation will be updated in #44357 (available in version 1.4) to reflect this. Closing.

@sasaki000
Copy link

sasaki000 commented Feb 28, 2022

A similar behavior has occurred in Pandas: [1.4.1].
Should I create another record?

import platform
import pandas as pd
print(f'- Python:[{platform.python_version()}], Pandas:[{pd.__version__}]') # <-- This AmbiguousTimeError occurred in Python:[3.9.1], Pandas:[1.4.1]

datetime_index = pd.DatetimeIndex([pd.Timestamp('2022-02-07',tz='America/New_York')], freq='D')
# datetime_index.freq = None # <--- You can avoid errors by executing this line.
print('Input:', datetime_index)

datetime_index = datetime_index.shift(periods=-92, freq='D')
print('Output:', datetime_index)
  • Python:[3.9.1], Pandas:[1.4.1]
    Input: DatetimeIndex(['2022-02-07 00:00:00-05:00'], dtype='datetime64[ns, America/New_York]', freq='D')

AmbiguousTimeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_4168/2621838263.py in
8 print('Input:', datetime_index)
9
---> 10 datetime_index = datetime_index.shift(periods=-92, freq='D')
11 print('Output:', datetime_index)

~\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\datetimelike.py in shift(self, periods, freq)
354 arr = self._data.view()
355 arr._freq = self.freq
--> 356 result = arr._time_shift(periods, freq=freq)
357 return type(self)._simple_new(result, name=self.name)
358

~\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\arrays\datetimelike.py in _time_shift(self, periods, freq)
1247 # appropriate timezone from start and end, so tz does not need
1248 # to be passed explicitly.
-> 1249 return self._generate_range(start=start, end=end, periods=None, freq=self.freq)
1250
1251 @unpack_zerodim_and_defer("add")

~\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\arrays\datetimes.py in _generate_range(cls, start, end, periods, freq, tz, normalize, ambiguous, nonexistent, inclusive)
453
454 if tz is not None and index.tz is None:
--> 455 arr = tzconversion.tz_localize_to_utc(
456 index.asi8, tz, ambiguous=ambiguous, nonexistent=nonexistent
457 )

~\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas_libs\tslibs\tzconversion.pyx in pandas._libs.tslibs.tzconversion.tz_localize_to_utc()

AmbiguousTimeError: Cannot infer dst time from 2021-11-07 01:00:00, try using the 'ambiguous' argument

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Frequency DateOffsets Timezones Timezone data dtype
Projects
None yet
Development

No branches or pull requests

6 participants