python - 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
解决方案
您可以按照错误中的建议进行操作:
df.index = pd.to_datetime(df.index, utc=True)
您还可以将时间转换为时-04:00
区tz_convert('US/Eastern')
推荐阅读
- scipy - scipy.optimize.minimize - 多变量优化
- java - 基于 DNS 的 WebRTC Java Firefox
- java - intellij idea 结构搜索和替换 - 查找所有调用 api 的方法
- python - 如何基于python创建模块化的html代码
- angular - ReplaySubject 上的方法 next() 在订阅中不起作用
- python - Python:如果异常,请在一定时间后重试
- android - Android运行ffmpeg示例程序构建报错
- c# - 如何在上下文中动态指定方法的泛型类型(如果可能,不进行反射)
- javascript - Laravel/FancyTree 中 Jquery 对 CSS 所做的更改仅反映在函数末尾
- android - 在父容器中包含 React Native SVGText