Jacob Bayer
Jacob Bayer

Reputation: 93

How to convert all pandas timestamp timezones in a pandas dataframe

Imagine you had a dataframe like this, with multiple datetime columns in UTC. What is the best way to convert them all to a timezone of your choice, such as EST?

import pandas as pd

df=pd.DataFrame({'id': {0: 12394, 1: 12393, 2: 12392, 3: 12391, 4: 12390},
 'created_timestamp': {0: pd.Timestamp('2020-12-30 02:13:19.921394'),
  1: pd.Timestamp('2020-12-30 02:10:36.176147'),
  2: pd.Timestamp('2020-12-30 02:05:56.261883'),
  3: pd.Timestamp('2020-12-30 02:02:06.483371'),
  4: pd.Timestamp('2020-12-30 02:01:10.212199')},
 'City': {0: 'Boston',
  1: 'New York',
  2: 'Albany',
  3: 'Buffalo',
  4: 'Detroit'},
 'updated_timestamp': {0: pd.Timestamp('2020-12-30 02:13:19.921394'),
  1: pd.Timestamp('2020-12-30 02:10:36.176147'),
  2: pd.Timestamp('2020-12-30 02:05:56.261883'),
  3: pd.Timestamp('2020-12-30 02:02:06.483371'),
  4: pd.Timestamp('2020-12-30 02:01:10.212199')}})

This is what I've tried:

timecols=df.select_dtypes(include=["datetime64[ns]"])

for col in timecols:
    df[col]=df[col] \
              .dt.tz_localize("UTC") \
              .dt.tz_convert("EST")

Is there a cleaner, more pythonic way of doing this?

Upvotes: 1

Views: 164

Answers (2)

Lambda
Lambda

Reputation: 1392

Try:

df.select_dtypes(include=["datetime64[ns]"]).apply(lambda x: x.dt.tz_localize("UTC").dt.tz_convert("US/Eastern"))

Upvotes: 1

FObersteiner
FObersteiner

Reputation: 25644

another option: make a boolean mask based on dtype, and use it in loc:

m = df.dtypes == 'datetime64[ns]'
df.loc[:, m] = df.loc[:, m].apply(lambda x: x.dt.tz_localize("UTC").dt.tz_convert("America/New_York"))

df
      id  ...                updated_timestamp
0  12394  ... 2020-12-29 21:13:19.921394-05:00
1  12393  ... 2020-12-29 21:10:36.176147-05:00
...

Upvotes: 1

Related Questions