Pandas convert datetime with a separate time zone column

前端 未结 2 939
渐次进展
渐次进展 2020-12-07 02:32

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

2条回答
  •  一生所求
    2020-12-07 03:35

    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')])
    

提交回复
热议问题