【问题标题】:AttributeError: 'numpy.int64' object has no attribute 'timestamp' in python 3.5 in AnacondaAttributeError:'numpy.int64'对象在Anaconda的python 3.5中没有属性'timestamp'
【发布时间】:2017-05-16 08:54:50
【问题描述】:

我是学习机器学习的新手,所以在 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 = df.iloc[-1].name做什么?这可以让您获得数据框中最后一行的 index,大概是一些np.int64,这对于索引是有意义的。为什么你期望它有一个timestamp 属性?
  • @SekarRaj:感谢您想要编辑。我拒绝了您的编辑,因为inline code formatting 用于代码和 IO,而不是任何东西的荧光笔。 “code”、“error”和“Python”之类的词本身不是code或IO,所以请不要格式化。
  • 医学博士。 E. S. C. - 请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。
  • @halfer:感谢您的编辑。下次我会记住这些事情
  • @juanpa.arrvillag,教程显示整数具有 timestamp() 属性,但没有明确给出实际原因。根据代码,next_unix实际上是某种日期格式的第二天。现在如何实现呢?

标签: python numpy anaconda


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      嗨,有时当做这样的操作时索引会改变。 Date 列不再是索引。

       df=df.set_index('Date')
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-24
        • 2016-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多