Pandas 如何将时间戳列 (EST) 转换为其他列中可用的本地时区信息
Pandas how to convert a timestamp column (EST) to local TimeZone info available in other column
来自
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
读取日期时间列作为时间戳并定位到 US/Eastern 时间,然后应用 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
来自
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
读取日期时间列作为时间戳并定位到 US/Eastern 时间,然后应用 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