1

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)?

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Tim
  • 12,318
  • 7
  • 50
  • 72

1 Answers1

3

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Oh, I didn't realize that timezone info is a part of the type itself. Thank you. – Tim Apr 11 '22 at 11:44