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

1
  • 1
    btw. you don't need to import pytz; you can do .tz_localize("UTC") :) Commented Apr 14, 2022 at 10:36

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I didn't realize that timezone info is a part of the type itself. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.