pandas, pytz - simple timezone convert

2.8k views Asked by At

I have the following dict:

{'actual': {Timestamp('2013-02-20 13:30:00'): 0.93000000000000005}}

I change it to a df:

df= pd.DataFrame.from_dict(dict, orient='columns', dtype=None)

if I perform:

df.index = df.index.tz_localize('US/Eastern').tz_convert(pytz.utc)

The df timestamp is correctly converted and returns:

                           actual
2013-02-20 18:30:00+00:00    0.93

However if I split the command in 2 lines such as :

df.index= df.index.tz_localize('US/Eastern')
df.index.tz_convert(pytz.utc)

it does not gets converted and returns:

                           actual
2013-02-20 13:30:00-05:00    0.93

Anyone knows why?

1

There are 1 answers

3
Psidom On BEST ANSWER

You just need to assign the index back to df in the second line:

import pytz
df.index = df.index.tz_localize('US/Eastern')
df.index = df.index.tz_convert(pytz.utc)

df
#                         actual
#2013-02-20 18:30:00+00:00  0.93