I have pandas DataFrames with time-series that cover middle european daylight saving time disturbances each spring and autum for a few years.
For some dfs, everything works as expected using dt.tz_localize but I have troubles solving the following time notation:
not_working = pd.to_datetime(
pd.Series(
[
"2017-10-29 01:45:00",
"2017-10-29 02:00:00",
"2017-10-29 02:15:00",
"2017-10-29 02:45:00",
"2017-10-29 03:00:00",
"2017-10-29 02:15:00",
"2017-10-29 02:45:00",
"2017-10-29 03:00:00",
]
)
)
not_working = not_working.dt.tz_localize(tz="Europe/Berlin", ambiguous="infer")
which results in this error message:
pytz.exceptions.AmbiguousTimeError: 2017-10-29 02:00:00
When localizing 02:00 twice instead, it works as expected. Sadly, I can't control the input data format. Thus, I am wondering how to localize my data above.
working = pd.to_datetime(
pd.Series(
[
"2017-10-29 01:45:00",
"2017-10-29 02:00:00",
"2017-10-29 02:15:00",
"2017-10-29 02:45:00",
"2017-10-29 02:00:00",
"2017-10-29 02:15:00",
"2017-10-29 02:45:00",
"2017-10-29 03:00:00",
]
)
)
working = working.dt.tz_localize(tz="Europe/Berlin", ambiguous="infer")
This fix works, but I'd prefer a less hacky solution. The nonexisting parameter does not work together with ambigous as I don't want to build a boolean array first.
not_working = not_working - pd.Timedelta(seconds=1)
not_working = not_working.dt.tz_localize(tz="Europe/Berlin", ambiguous="infer")
not_working = not_working + pd.Timedelta(seconds=1)