造成什么原因"属性错误:' numpy.ndarray'对象没有属性'差异'"

时间:2018-03-07 22:48:05

标签: python-3.x numpy

我是numpy的新手,我不了解有关diff的文档。下面的代码抛出错误。我很困惑任何帮助将不胜感激。

Traceback (most recent call last):
   File "/home/dave/Desktop/mcmtest/testhv calc.py", line 11, in <module>
     r =  np.log(close_prices).diff()
 AttributeError: 'numpy.ndarray' object has no attribute 'diff'

这是测试代码。

import numpy as np
from numpy import sqrt,mean,log,diff
import pandas as pd


close_prices = [178.97,175.5,171.07,171.85,172.43,172.99,167.37,164.34,162.71,\
                    156.41,155.15,159.54,163.03,156.49,160.5,167.78,167.43,166.97,167.96,171.51,171.11]

print (close_prices)

r =  np.log(close_prices).diff()
print(r)

1 个答案:

答案 0 :(得分:1)

鉴于numpy.ndarray是“numpy数组”的Python类型,错误只是说数组没有diff方法。 diffnumpy模块中定义的函数。

而不是np.log(close_prices).diff()

np.diff(np.log(close_prices))