Matias Ordoñez
Matias Ordoñez

Reputation: 105

Change timezone Pandas

I'm working with dates in Pandas, the data is saved as UTC in a MongoDB Database.

I'd like to convert it to localtime , this is what I try and it works but the problem is when I want to change the timezone in several columns of my Dataframe :

dfquestions=dfquestions.set_index('date_created')
dfquestions=dfquestions.tz_localize('UTC')
dfquestions=dfquestions.tz_convert('America/Argentina/Buenos_Aires')

If I try to do the same with another column I lose the indexed column.

Example:

dfquestions=dfquestions.set_index('date_created')
dfquestions=dfquestions.tz_localize('UTC')
dfquestions=dfquestions.tz_convert('America/Argentina/Buenos_Aires')
dfquestions=dfquestions.set_index('answer_date_created')
dfquestions=dfquestions.tz_localize('UTC')
dfquestions=dfquestions.tz_convert('America/Argentina/Buenos_Aires')

And I really don't think this is the best way to do it.

Upvotes: 2

Views: 180

Answers (1)

Leonardus Chen
Leonardus Chen

Reputation: 1254

No need to set_index before applying timezone localization/conversion.

def convert(x):  # Remove `pd.to_datetime()` if dtype is already a datetime
    return pd.to_datetime(x).dt.tz_localize('UTC').dt.tz_convert('America/Argentina/Buenos_Aires')

Example

data = {
    'date_created': ['2020-01-01 14:00:00', '2020-03-03 14:00:00', '2020-04-30 14:00:00'],
    'answer_date_created': ['2020-01-01 14:00:00', '2020-03-03 14:00:00', '2020-04-30 14:00:00'],
}
df = pd.DataFrame(data)
cols = ['date_created', 'answer_date_created']
df[cols].apply(convert, axis=1)

Output

               date_created       answer_date_created
0 2020-01-01 11:00:00-03:00 2020-01-01 11:00:00-03:00
1 2020-03-03 11:00:00-03:00 2020-03-03 11:00:00-03:00
2 2020-04-30 11:00:00-03:00 2020-04-30 11:00:00-03:00

Upvotes: 1

Related Questions