熊猫索引:将本地化对象转换为日期时间

时间:2019-11-18 16:42:58

标签: python python-3.x pandas dataframe

我有一个如下数据框:

                        A   
2014-06-02 09:00:00-04:00   ...
2014-06-02 10:00:00-04:00   ...
2014-06-02 11:00:00-04:00   ...
2014-06-02 12:00:00-04:00   ...

我需要将索引作为日期时间以进行一些操作,所以我正在尝试:

df.index = pd.to_datetime(df.index)

但是我遇到了以下错误

ValueError: Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True

请注意,索引已本地化(-04:00)

熊猫0.24.2

1 个答案:

答案 0 :(得分:2)

您可以遵循错误提示:

df.index = pd.to_datetime(df.index, utc=True)

您还可以将时间转换为带有-04:00的{​​{1}}时区