I have some data as shown below with hour
in UTC. I want to create a new column named local_hour
based on time_zone
. How can I do that? It seems like pandas' tz_convert
does not allow a column or pandas series as input to the tz
argument.
# 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')
dfhour 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().