Home > Enterprise >  Combine datetime and timezone columns in a Pandas DataFrame (tz_localize from column)
Combine datetime and timezone columns in a Pandas DataFrame (tz_localize from column)

Time:04-12

As already answered (Converting time zone pandas dataframe), Pandas provides means to localize datetime columns (tz_localize) and convert time zones (tz_convert) to a predefined time zone. For example:

df["localized_time"] = df.time.tz_localize(pytz.utc).tz_convert("Some/Timezone")

However, both functions accept the time zone itself as an argument. What if the time zone comes from another column in the same data frame? For example:

   time                 timezone        ...
0  2022-04-11 12:24:43  "Europe/Paris"  ...
1  2022-04-11 04:22:12  "US/Eastern"    ...
...

Is there a simple way to combine the "time" column (already a datetime type) with the time zone taken the "timezone" column (string)?

CodePudding user response:

Yes, it is possible, e.g. by:

df["localized_time"] = df.time.dt.tz_localize(pytz.utc)

df['new'] = df.apply(lambda x: x["localized_time"].tz_convert(x["timezone"]), 1)
print (df)
                 time      timezone            localized_time  \
0 2022-04-11 12:24:43  Europe/Paris 2022-04-11 12:24:43 00:00   
1 2022-04-11 04:22:12    US/Eastern 2022-04-11 04:22:12 00:00   

                         new  
0  2022-04-11 14:24:43 02:00  
1  2022-04-11 00:22:12-04:00 

But because there are different timezones get objects instead datetimes dtype:

print (df.dtypes)
time                   datetime64[ns]
timezone                       object
localized_time    datetime64[ns, UTC]
new                            object
dtype: object
  • Related