pandas_datareader.data manipulate data to an array

66 Views Asked by At

I'm studying Python for Finance and I'm trying to replicate a program from this book but the experiment was unsuccessful. I need the program to return an array of Adj Close but didnt find a way to do it with success.

This is the program from the book:

from matplotlib.finance import quotes_historical_yahoo_ochl as getData
ticker = 'IBM'
begdate = (2015,1,1)
enddate = (2015,11,9)
p = getData(ticker, begdate, enddate,asobject=True, adjusted=True)
ret = p.aclose[1:]/p.aclose[:-1]-1

This is my program:

import datetime
import pandas_datareader.data as web
import yfinance as yf
yf.pdr_override()

ticker = 'IBM'
start = datetime.datetime(2000, 6, 1)
end = datetime.datetime(2022, 6, 1)
msft = web.DataReader(ticker, start, end, asobject=True, adjusted=True)
ret = msft.aclose[1:] / msft.aclose[:-1] - 1
print(ret)

The error I get:

Traceback (most recent call last):
msft = web.DataReader(ticker, start, end, asobject=True, adjusted=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: download() got an unexpected keyword argument 'asobject'

Manually, I would retun something like this:

>>p.aclose[0:4]
array([ 151.174636, 148.795914, 145.586986, 144.635494])>>>
Sources of Data
[ 118 ]
>>>ret[0:3]
array([-0.01573493, -0.02122663, -0.00629399])
>>> (p.aclose[1]-p.aclose[0])/p.aclose[0]
-0.01573492791475934

Any advice would be appreciated.

1

There are 1 best solutions below

0
Matmozaur On

there are a few problems with your code, first as the error indicates there is no asobject parameter for the DataReader, and also no adjusted parameter. Your results will be downloaded in the form of Dataframe which you can later cast to numpy array, as is expected by you. I believe what you want to do should look like that:

import datetime
import pandas_datareader.data as web
import yfinance as yf
yf.pdr_override()

ticker = 'IBM'
start = datetime.datetime(2000, 6, 1)
end = datetime.datetime(2022, 6, 1)
msft = web.DataReader(ticker, start, end, auto_adjust=True)

ret = msft['Close'][1:].to_numpy() / msft['Close'][:-1].to_numpy() - 1
print(ret)