简体   繁体   中英

Convert the UTC datetime to local datetime in pandas

I have a data frame that consists the UTC_Time and the Timezone_info. I need to convert the UTC_Time to local_time. I have the following code but it is not working. Is there a way to do this efficiently (I can use a for loop and it works but I want to avoid for loop if possible).

        UTC_Time         Timezone_info
0  2018-02-12 18:16:00       America/New_York
1  2018-02-15 11:39:00    America/Puerto_Rico
2  2018-02-15 22:17:00    America/Los_Angeles
3  2018-02-17 00:59:00      America/Guayaquil
4  2018-02-17 11:34:00  America/Santo_Domingo

The code I am trying to use is: data['local_time']=data['UTC_Time'].dt.tz_localize('UTC').dt.tz_convert(data['Timezone_info'])

But this does not work.

The for loop that makes it work (but is probably the slowest way to do it is):

data['local_time']=0
for i in range(len(data)):
    tz=data.at[i,'Timezone_info']
    data.at[i,'local_time']=data.at[i,'UTC_Time'].tz_localize(data).tz_convert(tz)

What would be the pythonic way to do it?

Since tz_convert only takes one time zone as an argument it isn't "vectorized" on it's argument.

You can still use tz_convert in a vectorized form but first you have to use group_by to split the input based on the time zone.

data['local_time'] = (
    data['UTC_Time'].dt.tz_localize('UTC').       # Localize to UTC Time
    groupby(data['Timezone_info']).               # Group by time zone
    transform(lambda g: g.dt.tz_convert(g.name))) # Convert each group to local time zone

It took me a bit of experimentation to find that the groupby key was available in the name member of the group object. That should be added to the documentation of GroupBy.transform .

Using df.apply might work. This is not vectorised but it does avoid an explicit for loop.

def converter(row):
    return row['UTC_Time'].tz_localize('UTC').tz_convert(row['Timezone_info'])

df['local_time'] = df.apply(converter, axis=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM