Converting from local time to UTC in pandas

I am working with pandas dataframes containing local (Chilean) dates and times, e.g.:

local_time
2/9/2023 23:33
2/9/2023 23:39
3/9/2023 1:00
3/9/2023 1:08

I used to convert these dates and times to UTC by applying a bulk time shift (pd.Timedelta(4, "h")) to my local_time column, but now I would like to account for changes in daylight saving times.

I started using tz_location to specify the time zone before converting to UTC using tz_convert:

import pandas as pd
from datetime import datetime
df = pd.read_csv("Example_dataset.csv")
pd.to_datetime(pd.to_datetime(df['local_time']).apply(lambda x: datetime.strftime(x, '%d-%m-%Y %H:%M:%S'))).dt.tz_localize('America/Santiago').dt.tz_convert('UTC')

This seems to work on the example dataset provided here: data from September 2, 2023 are shifted by 4 hours, while those from September 3 are shifted by 3 hours.

0   2023-09-03 03:33:00+00:00
1   2023-09-03 03:39:00+00:00
2   2023-09-03 04:00:00+00:00
3   2023-09-03 04:08:00+00:00

Is this the correct way to proceed or am I missing anything here?

>Solution :

You need to localize then convert:

df["local_time"] = pd.to_datetime(df["local_time"]).dt.tz_localize("America/Santiago").dt.tz_convert("UTC")

Leave a Reply