20

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.

2
  • The problem here is that your timestamps have no offset so the only timezone you can localise to is GMT e.g. 'Europe/London', after which you can then call tz_convert('Europe/Berlin') this has nothing to do with 'Berlin', it would fail also if you tried to localise to 'Paris'
    – EdChum
    Aug 16, 2018 at 8:41
  • For instance if your timestamp was like this: '2017-10-29 02:04:15 +02:00' then your code would work
    – EdChum
    Aug 16, 2018 at 8:41

1 Answer 1

29

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.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.