属性错误: 'Timestamp'对象没有属性'timestamp'



我正在努力让我的代码运行。时间戳似乎有问题。你有什么建议,我可以改变我的代码吗?我知道以前有人问过这个问题,但没能成功。

这是我运行代码时得到的错误:'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

你可以试试:

import time
.....
last_unix = time.mktime(last_date.timetuple())

这对我有用!

我遇到了同样的问题,但是使用了Python 3.4,我使用了以下解决方案:

last_unix = (last_date - datetime.datetime(1970,1,1)).total_seconds()

我也遇到过同样的问题。我需要在python2和python3环境中提供对timestamp的访问(timestamps不是我的模块的主要目的)。

在多次尝试提供兼容性之后,我已经安装了 arrow。它已经经过了测试和pypi发布,并且可以在许多版本的Python上运行。

安装

Install from 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 ...

此外,还有很多漂亮和有用的功能(例如-没有更多的折磨与时区)。

相关内容

最新更新