nanuuq
nanuuq

Reputation: 189

Change timezone info for multiple datetime columns in pandas

Is there a easy way of converting all timestamp columns in a dataframe to local/any timezone?

Not by doing it column by column?

Upvotes: 6

Views: 1742

Answers (1)

cs95
cs95

Reputation: 402483

You can selectively apply the conversion to all datetime columns. First, select them with select_dtypes, then call tz_convert inside apply:

df2 = df.select_dtypes('datetimetz')
df[df2.columns] = df2.apply(lambda x: x.dt.tz_convert(None))

"datetimetz" selects all datetime columns with timezone information.
Replace tz_convert(None) with tz_convert(your_timezone) to convert to a different timezone.

For plain datetime objects without any timezone info, call tz_localize first with your current timezone before calling tz_convert.

Upvotes: 3

Related Questions