首页 > 解决方案 > 如何将纪元时间的熊猫索引转换为常规时间?

问题描述

我有一个带有纪元时间索引的熊猫数据框,价格如下:

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()。我彻底困惑和沮丧。我认为熊猫会让这样的事情变得微不足道。

感谢任何帮助,因为我希望对其运行超级简单的自相关并看看我得到了什么。

标签: python-3.x

解决方案


您可以使用以下代码段

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

推荐阅读