Pandas: How to convert local time to utc?

Viewed 24

I have a pandas dataframe where I have a column consisting of strings of date times, these I have converted to datetime with pd.to_datetime(). Then I have tried to convert this column to UTC from Swedish Local time, but get an NonExistentTimeError when converting with pandas. How do I resolve this without removing timestamps?

df['Time'] = pd.to_dataframe(df['Time'])
df['Time'] = df['Time'].dt.tz_localize('Europe/Stockholm')

My dataframe looks like this:

Time value
2022-03-26 23:00:00 1
2022-03-27 00:00:00 5
2022-03-27 01:00:00 6
2022-03-27 02:00:00 8
2022-03-27 03:00:00 4
2022-03-27 04:00:00 11
2022-03-27 05:00:00 10
1 Answers

As the error

NonExistentTimeError: 2022-03-27 02:00:00

tells you, that time did not exist in Stockholm local time. Due to the DST change, the hour after 1 am was 3 am, not 2 am. So to me, this looks like something wrong is happening during generation of this data, maybe worth investigating.

Anyways, what you can do is set nonexistent keyword, e.g. like

df['Time'] = pd.to_datetime(df['Time'])
df['Time'] = df['Time'].dt.tz_localize('Europe/Stockholm', nonexistent='NaT')

df['Time']
0   2022-03-26 23:00:00+01:00
1   2022-03-27 00:00:00+01:00
2   2022-03-27 01:00:00+01:00
3                         NaT
4   2022-03-27 03:00:00+02:00
5   2022-03-27 04:00:00+02:00
6   2022-03-27 05:00:00+02:00
Name: Time, dtype: datetime64[ns, Europe/Stockholm]

see the docs for different options.

Related