Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert UTC timestamp to local timezone issue in pandas

Tags:

python

pandas

I'm trying to convert a Unix UTC timestamp to a local date format in Pandas. I've been looking through a few solutions but I can't quite get my head around how to do this properly.

I have a dataframe with multiple UTC timestamp columns which all need to be converted to a local timezone. Let's say EU/Berlin.

I first convert all the timestamp columns into valid datetime columns with the following adjustments:

df['date'] = pd.to_datetime(df['date'], unit='s') This works and gives me the following outcome e.g. 2019-01-18 15:58:25 if I know try to adjust the timezone for this Date Time I have tried both:

df['date'].tz_localize('UTC').tz_convert('Europe/Berlin') and df['date'].tz_convert('Europe/Berlin')

In both cases the error is: TypeError: index is not a valid DatetimeIndex or PeriodIndex and I don't understand why.

The problem must be that the DateTime column is not on the index. But even when I use df.set_index('date') and after I try the above options it doesn't work and I get the same error.

Also, if it would work it seems that this method only allows the indexed DateTime to be timezone adjusted. How would I then adjust for the other columns that need adjustment?

Looking to find some information on how to best approach these issues once and for all! Thanks

like image 726
WvonR Avatar asked Apr 04 '19 14:04

WvonR


1 Answers

You should first specify that it is a datetime by adding the .dt. to a non index

df['date'] = df['date'].dt.tz_localize('UTC').dt.tz_convert('Europe/Berlin')

This should be used if the column is not the index column.

like image 133
d_kennetz Avatar answered Oct 22 '22 07:10

d_kennetz