John Ludhi/nbshare.io: Pandas Datareader To Download Stocks Data From Google And Yahoo Finance

Pandas Datareader To Download Stocks Data From Google And Yahoo Finance

Datareader package can be used to access data from multiple sources such as yahoo and google finance…

To install the package, use pip

In [ ]:
!pip install pandas_datareader

Use DataReader to access data from yahoo finance

In [ ]:
import numpy as np
import pandas as pd

from pandas_datareader import data as wb
aapl = wb.DataReader('AAPL', data_source='yahoo', start='1995-1-1')

At the time of writing this notebook, there is a bug in DataReader because of which You might run in to following error

TypeError: string indices must be integers

How To Fix DataReader TypeError: string indices must be integers

Use the following snippet to work around the above error.

In [9]:
import numpy as np
import pandas as pd
import yfinance as yf
yf.pdr_override()
from pandas_datareader import data as wb
[*********************100%***********************]  1 of 1 completed
In [11]:
aapl = wb.DataReader('AAPL', data_source='yahoo', start='1995-1-1')
[*********************100%***********************]  1 of 1 completed
In [15]:
aapl
Out[15]:
Open High Low Close Adj Close Volume
Date
1995-01-03 0.347098 0.347098 0.338170 0.342634 0.288771 103868800
1995-01-04 0.344866 0.353795 0.344866 0.351563 0.296296 158681600
1995-01-05 0.350446 0.351563 0.345982 0.347098 0.292533 73640000
1995-01-06 0.371652 0.385045 0.367188 0.375000 0.316049 1076622400
1995-01-09 0.371652 0.373884 0.366071 0.367885 0.310052 274086400
2023-01-09 130.470001 133.410004 129.889999 130.149994 130.149994 70790800
2023-01-10 130.259995 131.259995 128.119995 130.729996 130.729996 63896200
2023-01-11 131.250000 133.509995 130.460007 133.490005 133.490005 69458900
2023-01-12 133.880005 134.259995 131.440002 133.410004 133.410004 71379600
2023-01-13 132.029999 134.919998 131.660004 134.759995 134.759995 57758000

7059 rows × 6 columns

Use DataReader to access data from google finance

In [13]:
googl = wb.DataReader('GOOGL', data_source='googl', start='1995-1-1')
[*********************100%***********************]  1 of 1 completed
In [14]:
googl
Out[14]:
Open High Low Close Adj Close Volume
Date
2004-08-19 2.502503 2.604104 2.401401 2.511011 2.511011 893181924
2004-08-20 2.527778 2.729730 2.515015 2.710460 2.710460 456686856
2004-08-23 2.771522 2.839840 2.728979 2.737738 2.737738 365122512
2004-08-24 2.783784 2.792793 2.591842 2.624374 2.624374 304946748
2004-08-25 2.626627 2.702703 2.599600 2.652653 2.652653 183772044
2023-01-09 88.360001 90.050003 87.860001 88.019997 88.019997 29003900
2023-01-10 85.980003 88.669998 85.830002 88.419998 88.419998 30467800
2023-01-11 89.180000 91.599998 89.010002 91.519997 91.519997 26862000
2023-01-12 91.480003 91.870003 89.750000 91.129997 91.129997 30258100
2023-01-13 90.849998 92.190002 90.129997 92.120003 92.120003 26309900

4634 rows × 6 columns

Note – The pandas_datareader library does not have a built-in function for retrieving options data. However, there are other libraries and APIs you can use to obtain options data, such as yfinance or alpha_vantage for Yahoo Finance and Alpha Vantage, iex for IEX Cloud API.

Check out following tutorial if you want to use yfinance
How To Use yfinance Package

Planet Python