I have a dataframe with a column for the time zone and a column for the datetime. I would like to convert these to UTC first to join with other data, and then I\'ll have som
Your issue is that tz_localize()
can only take a scalar value, so we'll have to iterate through the DataFrame:
df['datetime_utc'] = [d['datetime'].tz_localize(d['time_zone']).tz_convert('UTC') for i,d in df.iterrows()]
The result is:
datetime time_zone datetime_utc
0 2016-09-19 01:29:13 America/Bogota 2016-09-19 06:29:13+00:00
1 2016-09-19 02:16:04 America/New_York 2016-09-19 06:16:04+00:00
2 2016-09-19 01:57:54 Africa/Cairo 2016-09-18 23:57:54+00:00
An alternative approach is to group by the timezone and convert all matching rows in one pass:
df['datetime_utc'] = pd.concat([d['datetime'].dt.tz_localize(tz).dt.tz_convert('UTC') for tz, d in df.groupby('time_zone')])