首页 > 解决方案 > Pandas 索引:将本地化对象转换为日期时间

问题描述

我有一个如下数据框:

                        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

标签: pythonpython-3.xpandasdataframe

解决方案


您可以按照错误中的建议进行操作:

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

您还可以将时间转换为时-04:00tz_convert('US/Eastern')


推荐阅读