Pandas how to convert a timestamp column (EST) to local TimeZone info available in other column

笑着哭i 提交于 2019-12-11 01:51:31

问题


From

colA (EST)                 colB (local tz) 
2016-09-19 01:29:13        US/Central
2016-09-19 02:16:04        Etc/GMT+2
2016-09-19 01:57:54        Europe/London

To

colA (EST)                 colB (local tz)      colC (timestamp in local tz)    
2016-09-19 01:29:13        US/Central           2016-09-19 02:29:13 
2016-09-19 02:16:04        Etc/GMT+2            2016-09-19 08:16:04
2016-09-19 01:57:54        Europe/London        2016-09-19 05:57:54  

回答1:


Read the datetime column as timestamp and localize to US/Eastern time and then apply tz_convert

df['colA (EST)'] = pd.to_datetime(df['colA (EST)']).dt.tz_localize('US/Eastern')
df['colC (timestamp in local tz) '] = df.apply(lambda x: x['colA (EST)']\
.tz_convert(x['colB (local tz)']), axis = 1)



    colA (EST)                 colB (local tz)  colC (timestamp in local tz)
0   2016-09-19 01:29:13-04:00   US/Central      2016-09-19 00:29:13-05:00
1   2016-09-19 02:16:04-04:00   Etc/GMT+2       2016-09-19 04:16:04-02:00
2   2016-09-19 01:57:54-04:00   Europe/London   2016-09-19 06:57:54+01:00


来源:https://stackoverflow.com/questions/52546068/pandas-how-to-convert-a-timestamp-column-est-to-local-timezone-info-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!