python-3.x - 如何将纪元时间的熊猫索引转换为常规时间?
问题描述
我有一个带有纪元时间索引的熊猫数据框,价格如下:
df = pd.DataFrame({'Index': [1547942400000, 1548028800000, 1548115200000], 'Price': [1170, 1170, 1170]})
# EDIT the index column in unnamed.
我试过直接编辑索引,比如
df.index = dt.datetime.fromtimestamp(int(df.index) / 100).strftime('%Y-%m-%d')
# 100 to make it this year vs 1970s or 2040s
我也尝试过使用不能在索引上使用的 apply()。我彻底困惑和沮丧。我认为熊猫会让这样的事情变得微不足道。
感谢任何帮助,因为我希望对其运行超级简单的自相关并看看我得到了什么。
解决方案
您可以使用以下代码段
df = pd.DataFrame({'Index': [1547942400000, 1548028800000, 1548115200000], 'Price': [1170, 1170, 1170]})
df.index = pd.to_datetime(df['Index'],unit='ms')
df
Index Price
Index
2019-01-20 1547942400000 1170
2019-01-21 1548028800000 1170
2019-01-22 1548115200000 1170
推荐阅读
- flutter - 单击孙元素后颤动更改祖父容器
- next.js - 打开图表图像元标记,仅适用于 LinkedIn
- javascript - 图表峰值绘制得高于实际数据点
- python - 会话在抓取网站并在 PythonAnyWhere 服务器上运行时自动过期
- android - 无法获取 https:////dl.bintray.com/lukaville/maven/com/nbsp/library/1.09
- eclipse - 如何在同一个 IDE 中对两个 STM 微控制器进行编程
- javascript - 试图将 Sharepoint 列表链接到我的 React 网页
- postman - 检查参数大写字母,小写字母
- sql - t-sql中变量名后的平均名称是什么
- spring - 使用直接 GCP 云 Pubsub 或 Spring 集成的 Pubsub 消息传递的最佳实践是什么?