你好,我正在尝试将数据框的时区设置为并转换为UTC时区。
我要读取的数据是:
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
我使用这段代码
df = pd.read_csv('input.csv', sep=";", index_col='Date Time', decimal=',')
df.index = pd.to_datetime(df.index, unit='s')
我的想法是使用这段代码:
df.index.tz_localize('Europe/Rome').tz_covert('UTC')
如果我试图使用以下代码设置时区为
'Europe/Rome'
,则可以这样做:df.index=df.index.tz_localize('Europe/Rome')
我得到了这个结果:
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
有什么建议吗?
我确定输入文件中的时间设置正确,并且dst也没问题!
df.index = pd.to_datetime(df.index, unit='s').tz_localize(from_tz)
。文件路径为"/usr/local/lib/python2.7/dist-packages/pandas/util/decorators.py"
,第89行的wrapper函数返回了func函数的结果,而在"/usr/local/lib/python2.7/dist-packages/pandas/tseries/index.py"
文件中的第1724行,tz_localize函数出现了问题,其中ambiguous参数存在问题。在"pandas/tslib.pyx"
文件的第3781行,pandas.tslib.tz_localize_to_utc
函数出现了NonExistentTimeError错误,时间为2013-03-31 02:00:00
。 - Lorenzo Bottaccioli