如何使用 pandas tz_convert 转换为多个不同时区

Gau*_*sal 7 python pandas

我有一些数据,如下所示,采用hourUTC 格式。我想创建一个名为local_hour基于的新列time_zone。我怎样才能做到这一点?pandas 似乎tz_convert不允许列或 pandas 系列作为参数的输入tz

# Create dataframe
import pandas as pd
df = pd.DataFrame({
    'hour': ['2019-01-01 05:00:00', '2019-01-01 07:00:00', '2019-01-01 08:00:00'],
    'time_zone': ['US/Eastern', 'US/Central', 'US/Mountain']
})

# Convert hour to datetime and localize to UTC
df['hour'] = pd.to_datetime(df['hour']).dt.tz_localize('UTC')

df 
        hour                     time_zone
0   2019-01-01 05:00:00+00:00   US/Eastern
1   2019-01-01 07:00:00+00:00   US/Central
2   2019-01-01 08:00:00+00:00   US/Mountain

# Create local_hour column to convert hour to US/Eastern time (this works)
df['local_hour'] = df['hour'].dt.tz_convert(tz='US/Eastern')
df
    hour                        time_zone   local_hour
0   2019-01-01 05:00:00+00:00   US/Eastern  2019-01-01 00:00:00-05:00
1   2019-01-01 07:00:00+00:00   US/Central  2019-01-01 02:00:00-05:00
2   2019-01-01 08:00:00+00:00   US/Mountain 2019-01-01 03:00:00-05:00

# Try to create local_hour column to convert hour based on time_zone column (this fails) 
df['local_hour'] = df['hour'].dt.tz_convert(tz=df['time_zone'])
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Run Code Online (Sandbox Code Playgroud)

Cod*_*ent 7

dt.tz_convert其参数需要一个标量值tz,而不是类似时区的值的列表。使用apply,它本质上是一个循环:

df['local_hour'] = df.apply(lambda row: row['hour'].tz_convert(row['time_zone']), axis=1)
Run Code Online (Sandbox Code Playgroud)