Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get 'Cannot infer dst time from '2017-10-29 02:04:15', try using the 'ambiguous' argument?

I managed to create a minimal example that shows the same behaviour:

>>> import pandas as pd
>>> df = pd.DataFrame({'id': [1], 'date': ['2017-10-29 02:04:15']})
>>> df['date'] = pd.to_datetime(df['date'])
>>> df['date'].dt.tz_localize('Europe/Berlin')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/math/.local/lib/python3.5/site-packages/pandas/core/accessor.py", line 88, in f
    return self._delegate_method(name, *args, **kwargs)
  File "/home/math/.local/lib/python3.5/site-packages/pandas/core/indexes/accessors.py", line 99, in _delegate_method
    result = method(*args, **kwargs)
  File "/home/math/.local/lib/python3.5/site-packages/pandas/core/indexes/datetimes.py", line 2368, in tz_localize
    errors=errors)
  File "pandas/_libs/tslibs/conversion.pyx", line 977, in pandas._libs.tslibs.conversion.tz_localize_to_utc
pytz.exceptions.AmbiguousTimeError: Cannot infer dst time from '2017-10-29 02:04:15', try using the 'ambiguous' argument

Why does this error occur?

The documentation of tz_localize doesn't explain this properly.

like image 517
Martin Thoma Avatar asked Aug 16 '18 08:08

Martin Thoma


1 Answers

The error message says that it cannot infer Daylight Saving Time. Now, as probably everybody knows, the change from Daylight Saving Time to normal time occurs at the end of October at 2:00am/3:00am, and in fact, a quick look at the calender tells us that DST change in 2017 was on October, 29th.

The change from Daylight Saving Time to normal time occurs this way:

  1. The clock runs on DST from 2:00am to 2:59:59.99999999…am
  2. At 3:00am sharp, the clock jumps to 2:00am again, now on normal time.

Therefore, there are two different times that the clock shows 2017-10-29 02:04:15. Or, in other words, the time 2017-10-29 02:04:15 is ambiguous without knowing whether you are talking about DST or not … which is exactly what the error is saying.

like image 98
Jörg W Mittag Avatar answered Oct 22 '22 18:10

Jörg W Mittag