NbShare
  • Nbshare Notebooks

  • Table of Contents

  • Python Utilities

    • How To Install Jupyter Notebook
    • How to Upgrade Python Pip
    • How To Use Python Pip
  • Python

    • Python Datetime
    • Python Dictionary
    • Python Generators
    • Python Iterators and Generators
    • Python Lambda
    • Python Sort List
    • String And Literal In Python 3
    • Strftime and Strptime In Python
    • Python Tkinter
    • Python Underscore
    • Python Yield
  • Pandas

    • Aggregating and Grouping
    • DataFrame to CSV
    • DF to Numpy Array
    • Drop Columns of DF
    • Handle Json Data
    • Iterate Over Rows of DataFrame
    • Merge and Join DataFrame
    • Pivot Tables
    • Python List to DataFrame
    • Rename Columns of DataFrame
    • Select Rows and Columns Using iloc, loc and ix
    • Sort DataFrame
  • PySpark

    • Data Analysis With Pyspark
    • Read CSV
    • RDD Basics
  • Data Science

    • Confusion Matrix
    • Decision Tree Regression
    • Logistic Regression
    • Regularization Techniques
    • SVM Sklearn
    • Time Series Analysis Using ARIMA
  • Machine Learning

    • How To Code RNN and LSTM Neural Networks in Python
    • PyTorch Beginner Tutorial Tensors
    • Rectified Linear Unit For Artificial Neural Networks Part 1 Regression
    • Stock Sentiment Analysis Using Autoencoders
  • Natural Language
    Processing

    • Opinion Mining Aspect Level Sentiment Analysis
    • Sentiment Analysis using Autoencoders
    • Understanding Autoencoders With Examples
    • Word Embeddings Transformers In SVM Classifier
  • R

    • DataFrame to CSV
    • How to Create DataFrame in R
    • How To Use Grep In R
    • How To Use R Dplyr Package
    • Introduction To R DataFrames
    • Tidy Data In R
  • A.I. News
NbShare Notebooks
  • Publish Your Post On nbshare.io

  • R Python Pandas Data Science Excel NLP Numpy Pyspark 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

Related Notebooks

  • How To Analyze Yahoo Finance Data With R
  • Calculate Stock Options Max Pain Using Data From Yahoo Finance With Python
  • How To Calculate Stocks Support And Resistance Using Clustering
  • How To Parse Yahoo Finance News Feed With Python
  • Summarising Aggregating and Grouping data in Python Pandas
  • How to Analyze the CSV data in Pandas
  • How To Read JSON Data Using Python Pandas
  • How To Analyze Wikipedia Data Tables Using Python Pandas
  • TTM Squeeze Stocks Scanner Using Python

Register

User Already registered.


Login

Login

We didn't find you! Please Register

Wrong Password!


Register
    Top Notebooks:
  • Data Analysis With Pyspark Dataframe
  • Strftime and Strptime In Python
  • Python If Not
  • Python Is Integer
  • Dictionaries in Python
  • How To install Python3.9 With Conda
  • String And Literal In Python 3
  • Privacy Policy
©