Python - pandas datetime column with multiple timezones

前端 未结 2 1607
南方客
南方客 2021-01-22 15:10

I have a data frame with multiple users and timezones, like such:

cols = [\'user\', \'zone_name\', \'utc_datetime\']
data = [
    [1, \'Europe/Amsterdam\', pd.to         


        
2条回答
  •  梦谈多话
    2021-01-22 16:03

    Relatively shorter answer using DataFrame.apply:

    df['local_datetime'] = df.apply(lambda x: x.utc_datetime.tz_localize(tz = "UTC").tz_convert(x.zone_name), axis = 1)
    print(df)
       user         zone_name        utc_datetime             local_datetime
    0     1  Europe/Amsterdam 2019-11-13 11:14:15  2019-11-13 12:14:15+01:00
    1     2     Europe/London 2019-11-13 11:14:15  2019-11-13 11:14:15+00:00
    

    If you want to remove the time zone information, you can localize times by passing None

    df['local_datetime'] = df.apply(lambda x: x.utc_datetime.tz_localize(tz = "UTC").tz_convert(x.zone_name).tz_localize(None), axis = 1)
    print(df)
       user         zone_name        utc_datetime      local_datetime
    0     1  Europe/Amsterdam 2019-11-13 11:14:15 2019-11-13 12:14:15
    1     2     Europe/London 2019-11-13 11:14:15 2019-11-13 11:14:15
    

提交回复
热议问题