AttributeError: 'numpy.int64' object has no attribute 'timestamp' in python 3.5 in Anaconda

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

I am a newbie in learning Machine Learning, so following a great tutorial in YouTube. But the following code is giving me an error. I read a similar question in here, but timetuple() does not solve my case, nor any solutions from the video.

Here is my code :

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 

For this above code, I got this following error :

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' 

I couldn't figure out the solution though there are many solutions in the internet but nothing worked for me. I am using Python 3.5 in anaconda. timetuple() doesn't work for me and same attribute error occurs.

回答1:

It seems it doesn't index the rows by the dates. So when you are trying to get last_date, actually it is getting int instead of date.

As per my understanding you can add date index by using the following line after reading csv code - df.set_index('date', inplace=True)



回答2:

Replace last_date.timestamp() with time.mktime(datetime.datetime.strptime(last_date,"%d%m%y").timetuple())



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!