我正在努力让我的代码运行起来。时间戳似乎有问题。对于如何更改代码,您有什么建议吗?我看到以前有人问过这个问题,但没能让它起作用。
这是我在运行代码时得到的错误:'Timestamp' object has no attribute 'timestamp'
我的代码:
import quandl, math, datetime
last_date = df.iloc[-1].name
last_unix = last_date.timestamp()
one_day = 86400 #Seconds in a day
next_unix = last_unix + one_day
for i in forecast_set:
next_date = datetime.datetime.fromtimestamp(next_unix)
next_unix += one_day
df.loc[next_date]=[np.nan for _ in range(len(df.columns)-1)]+[i]
#Loop to replace all numbers on x axis with dates发布于 2017-03-06 16:04:23
您可以尝试这样做:
import time
.....
last_unix = time.mktime(last_date.timetuple())这对我很有效!
发布于 2017-05-12 16:03:29
我也有同样的问题,但在Python3.4中,我使用了以下解决方案。
last_unix = (last_date - datetime.datetime(1970,1,1)).total_seconds()发布于 2017-09-01 21:55:27
我也遇到过同样的问题。我需要在python2和python3环境中都提供对timestamp的访问(timestamps不是我的模块的主要目的)。
在几次尝试提供兼容性之后,我安装了 package。它已经过测试和pypi发布,可以在许多版本的Python上运行。
安装
从PyPI安装
pip install arrow或添加到依赖项中
在代码中使用
import arrow
import quandl, math, datetime
last_date = df.iloc[-1].name
# use arrow
last_unix = arrow.get(last_date).timestamp
one_day = 86400 #Seconds in a day
next_unix = last_unix + one_day
# ... your code ...此外,还有许多漂亮和有用的功能(例如-不再有时区的折磨)。
https://stackoverflow.com/questions/40473442
复制相似问题