Reputation: 441
Hi I'm trying to set the timezone to the dataframe and then changeit to UCT time zone.
The data I'm reading is:
Date Time;G_h
2012-03-31 23:00:00.000;0
2012-03-31 23:15:00.000;0
2012-03-31 23:30:00.000;0
2012-03-31 23:45:00.000;0
2012-04-01 00:00:00.000;0
2012-04-01 00:15:00.000;0
2012-04-01 00:30:00.000;0
2012-04-01 00:45:00.000;0
2012-04-01 01:00:00.000;0
2012-04-01 01:15:00.000;0
2012-04-01 01:30:00.000;0
I' use this code
df = pd.read_csv('input.csv', sep=";", index_col='Date Time', decimal=',')
df.index = pd.to_datetime(df.index, unit='s')
My idea was to use this code:
df.index.tz_localize('Europe/Rome').tz_covert('UTC')
If I try to set the timezone 'Europe/Rome'
with this code
df.index=df.index.tz_localize('Europe/Rome')
I get this result:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2820, in run_code
exec code_obj in self.user_global_ns, self.user_ns
File "<ipython-input-38-0aa69957bc90>", line 1, in <module>
d.index.tz_localize('Europe/Rome')
File "/usr/local/lib/python2.7/dist-packages/pandas/tseries/index.py", line 1608, in tz_localize
new_dates = tslib.tz_localize_to_utc(self.asi8, tz, infer_dst=infer_dst)
File "tslib.pyx", line 1981, in pandas.tslib.tz_localize_to_utc (pandas/tslib.c:29912)
AmbiguousTimeError: Cannot infer dst time from Timestamp('2012-10-28 02:00:00', tz=None), try using the 'infer_dst' argument
Any suggestion?
I'm sure that the time is set correctly in the input file and the dst is ok!
Upvotes: 3
Views: 3047
Reputation: 210822
try this:
from __future__ import print_function
import pytz
import pandas as pd
from_tz = pytz.timezone('Europe/Rome')
to_tz = pytz.timezone('UTC')
df = pd.read_csv('input.csv', sep=";", index_col='Date Time', decimal=',')
df.index = pd.to_datetime(df.index, unit='s').tz_localize(from_tz).tz_convert(to_tz)
print(df)
Output:
G_h
Date Time
2012-03-31 21:00:00+00:00 0
2012-03-31 21:15:00+00:00 0
2012-03-31 21:30:00+00:00 0
2012-03-31 21:45:00+00:00 0
2012-03-31 22:00:00+00:00 0
2012-03-31 22:15:00+00:00 0
2012-03-31 22:30:00+00:00 0
2012-03-31 22:45:00+00:00 0
2012-03-31 23:00:00+00:00 0
2012-03-31 23:15:00+00:00 0
2012-03-31 23:30:00+00:00 0
Upvotes: 1