AttributeError:'numpy.int64'对象在 Python 3.5 中的 Anaconda 中没有属性'timestamp'



我是学习机器学习的新手,所以在YouTube上遵循了一个很棒的教程。但是下面的代码给了我一个错误。我在这里读到了一个类似的问题,但timetuple()并不能解决我的情况,也没有解决视频中的任何解决方案。

这是我的代码:

import pandas as pd
import quandl, math
from datetime import datetime, date, time, timedelta
import time
import numpy as np
from sklearn import preprocessing, cross_validation, svm
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt  #plot stuff, how to plot in graph
from matplotlib import style     #nice looking thing 
style.use('ggplot')              #which nice-looking-thing i wanna use
quandl.ApiConfig.api_key = '...' #erased my key for secrecy
df = quandl.get_table('WIKI/PRICES')

## ... ##other irrelevant code snippets
forecast_out = int(math.ceil(0.01*len(df)))
df['label'] = df[forecast_col].shift(-forecast_out)
X = np.array(df.drop(['label'],1)) 
X = preprocessing.scale(X)
X_lately = X[-forecast_out:]    
X = X[:-forecast_out]
df.dropna(inplace=True)
y = np.array(df['label'])
y = np.array(df['label'])
# ... #other irrelevant code snippets
forecast_set = clf.predict(X_lately)
df['Forecast'] = np.nan
last_date = df.iloc[-1].name
last_unix = last_date.timestamp() ###MAIN attribute error found here
one_day = 86400
next_unix = last_unix + one_day

对于上面的代码,我收到以下错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-8-4a1a193ea81d> in <module>()
      1 last_date = df.iloc[-1].name
----> 2 last_unix = last_date.timestamp()
      3 one_day = 86400
      4 next_unix = last_unix + one_day
AttributeError: 'numpy.int64' object has no attribute 'timestamp'

我无法找出解决方案,尽管互联网上有很多解决方案,但没有任何效果。我在anaconda中使用Python 3.5。 timetuple()对我不起作用,并且发生相同的属性错误。

它似乎没有按日期索引行。因此,当您尝试获取last_date时,实际上它正在获取 int 而不是日期。

根据我的理解,您可以在阅读csv代码后使用以下行添加日期索引 - df.set_index('date',inplace=True(

last_date.timestamp()替换为time.mktime(datetime.datetime.strptime(last_date,"%d%m%y").timetuple())

嗨,有时何时更改此类操作索引。 Date列不再是索引。

 df=df.set_index('Date')

只需在创建dataframe后添加此内容即可。
例-
df = quandl.get_table('WIKI/PRICES')或在此之后添加df=pd.read_csv("stock_data.csv")

最新更新