2017-05-16 30 views
0

我是學習機器學習的新手,所以遵循YouTube上的一個很棒的教程。但是下面的代碼給了我一個錯誤。我在這裏閱讀了一個類似的問題,但timetuple()沒有解決我的情況,也沒有解決任何來自視頻的解決方案。AttributeError:'numpy.int64'對象在python 3.5中沒有屬性'timestamp'在Anaconda

這裏是我的代碼:

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()不適用於我,併發生相同的屬性錯誤。

+0

什麼是你想用'LAST_DATE做= df.iloc [-1] .name'?這會得到你數據框中最後一行的* index *,大概是'np.int64',這對索引是有意義的。你爲什麼期望它有'timestamp'屬性? –

+0

@SekarRaj:謝謝你想編輯。我已經拒絕了你的編輯,因爲「內聯代碼格式化」用於代碼和IO,而不是任何事物的高亮顯示。諸如「代碼」,「錯誤」和「Python」之類的詞本身不是代碼或IO,因此請將它們設置爲無格式。 – halfer

+0

Md。ESC - 請閱讀[在什麼情況下,我可以在我的問題中添加「緊急」或其他類似短語,以獲得更快的答案?](https://meta.stackoverflow.com/q/326569) - 總之是,這不是解決志願者問題的理想方式,而且可能適得其反。請不要將這添加到您的問題。 – halfer

回答

0

看來它不會按日期對行進行索引。所以當你試圖獲取last_date時,實際上它是獲取int而不是日期。

按我的理解,你可以用下面一行添加日期索引讀取CSV代碼之後 - df.set_index(「日期」,就地= TRUE)

相關問題