将时区设置为pandas数据框

3

你好,我正在尝试将数据框的时区设置为并转换为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也没问题!

1个回答

1

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)

输出:

                           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

你使用的Pandas版本是哪个? - MaxU - stand with Ukraine
In[13]: pd.version Out[13]: '0.13.1' - Lorenzo Bottaccioli
哦,我明白了。我有0.17.1版本。你有升级的机会吗? - MaxU - stand with Ukraine
抱歉回复晚了,我已将pandas更新到0.17.1,但是出现了以下错误: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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接