我有一个包含 unix 时间和价格的数据框。我想转换索引列,以便它以人类可读的日期显示。
So for instance I have date
as 1349633705
in the index column but I’d want it to show as 10/07/2012
(or at least 10/07/2012 18:15
)。
对于某些上下文,这是我正在使用的代码以及我已经尝试过的代码:
import json
import urllib2
from datetime import datetime
response = urllib2.urlopen('http://blockchain.info/charts/market-price?&format=json')
data = json.load(response)
df = DataFrame(data['values'])
df.columns = ["date","price"]
#convert dates
df.date = df.date.apply(lambda d: datetime.strptime(d, "%Y-%m-%d"))
df.index = df.date
如您所见,我在这里使用 df.date = df.date.apply(lambda d: datetime.strptime(d, "%Y-%m-%d"))
这不起作用,因为我使用的是整数,而不是字符串。我想我需要使用 datetime.date.fromtimestamp
但我不太确定如何将其应用于整个 df.date
。
谢谢。
原文由 W A Carnegie 发布,翻译遵循 CC BY-SA 4.0 许可协议
这些似乎是纪元以来的秒数。