sio2bagger
sio2bagger

Reputation: 119

what is causing "AttributeError: 'numpy.ndarray' object has no attribute 'diff'"

I am new to numpy and I am NOT understanding the documentation as regards diff. the code below throws the error. I am baffled any help would be appreciated.

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'

here is the test code.

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)

Upvotes: 5

Views: 23606

Answers (1)

Mad Physicist
Mad Physicist

Reputation: 114290

Given that numpy.ndarray is the Python type of "numpy arrays", the error is just saying that arrays don't have a diff method. diff is a function defined in the numpy module.

Instead of np.log(close_prices).diff(), do

np.diff(np.log(close_prices))

Upvotes: 4

Related Questions