Skip to content

Commit

Permalink
[pandas] update pandas to use yfinance and minor edits (#133)
Browse files Browse the repository at this point in the history
* update pandas to use yfinance and minor edits

* fix read function for yahoo finance

* remove margin as not supported in quantecon-book-theme

* add a space for title
  • Loading branch information
mmcky authored Apr 13, 2021
1 parent b0719b5 commit 47b29f4
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lectures/pandas.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ In addition to what’s in Anaconda, this lecture will need the following librar
tags: [hide-output]
---
!pip install --upgrade pandas-datareader
!pip install --upgrade yfinance
```

## Overview
Expand Down Expand Up @@ -385,18 +386,28 @@ Note that pandas offers many other file type alternatives.

Pandas has [a wide variety](https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html) of top-level methods that we can use to read, excel, json, parquet or plug straight into a database server.

### Using {index}`pandas_datareader <single: pandas_datareader>` to Access Data
### Using {index}`pandas_datareader <single: pandas_datareader>` and {index}`yfinance <single: yfinance>` to Access Data

```{index} single: Python; pandas-datareader
```

The maker of pandas has also authored a library called pandas_datareader that gives programmatic access to many data sources straight from the Jupyter notebook.
The maker of pandas has also authored a library called
[pandas_datareader](https://pandas-datareader.readthedocs.io/en/latest/) that
gives programmatic access to many data sources straight from the Jupyter notebook.

While some sources require an access key, many of the most important (e.g., FRED, [OECD](https://data.oecd.org/), [EUROSTAT](https://ec.europa.eu/eurostat/data/database) and the World Bank) are free to use.

We will also use [yfinance](https://pypi.org/project/yfinance/) to fetch data from Yahoo finance
in the exercises.

For now let's work through one example of downloading and plotting data --- this
time from the World Bank.

```{note}
There are also other [python libraries](https://data.worldbank.org/products/third-party-apps)
available for working with world bank data such as [wbgapi](https://pypi.org/project/wbgapi/)
```

The World Bank [collects and organizes data](http://data.worldbank.org/indicator) on a huge range of indicators.

For example, [here's](http://data.worldbank.org/indicator/GC.DOD.TOTL.GD.ZS/countries) some data on government debt as a ratio to GDP.
Expand Down Expand Up @@ -426,7 +437,7 @@ With these imports:

```{code-cell} python3
import datetime as dt
from pandas_datareader import data
import yfinance as yf
```

Write a program to calculate the percentage price change over 2019 for the following shares:
Expand Down Expand Up @@ -460,7 +471,8 @@ def read_data(ticker_list,
ticker = pd.DataFrame()
for tick in ticker_list:
prices = data.DataReader(tick, 'yahoo', start, end)
stock = yf.Ticker(tick)
prices = stock.history(start=start, end=end)
closing_prices = prices['Close']
ticker[tick] = closing_prices
Expand Down

2 comments on commit 47b29f4

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.