本文介绍了 pandas 如何将时间戳列(EST)转换为其他列中可用的本地TimeZone信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
来自
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
收件人
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
推荐答案
读取datetime列作为时间戳并本地化为美国/东部时间,然后应用tz_convert
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
这篇关于 pandas 如何将时间戳列(EST)转换为其他列中可用的本地TimeZone信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!