Skip to content

Commit 47b29f4

Browse files
authored
[pandas] update pandas to use yfinance and minor edits (#133)
* 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
1 parent b0719b5 commit 47b29f4

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

lectures/pandas.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ In addition to what’s in Anaconda, this lecture will need the following librar
3434
tags: [hide-output]
3535
---
3636
!pip install --upgrade pandas-datareader
37+
!pip install --upgrade yfinance
3738
```
3839

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

386387
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.
387388

388-
### Using {index}`pandas_datareader <single: pandas_datareader>` to Access Data
389+
### Using {index}`pandas_datareader <single: pandas_datareader>` and {index}`yfinance <single: yfinance>` to Access Data
389390

390391
```{index} single: Python; pandas-datareader
391392
```
392393

393-
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.
394+
The maker of pandas has also authored a library called
395+
[pandas_datareader](https://pandas-datareader.readthedocs.io/en/latest/) that
396+
gives programmatic access to many data sources straight from the Jupyter notebook.
394397

395398
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.
396399

400+
We will also use [yfinance](https://pypi.org/project/yfinance/) to fetch data from Yahoo finance
401+
in the exercises.
402+
397403
For now let's work through one example of downloading and plotting data --- this
398404
time from the World Bank.
399405

406+
```{note}
407+
There are also other [python libraries](https://data.worldbank.org/products/third-party-apps)
408+
available for working with world bank data such as [wbgapi](https://pypi.org/project/wbgapi/)
409+
```
410+
400411
The World Bank [collects and organizes data](http://data.worldbank.org/indicator) on a huge range of indicators.
401412

402413
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.
@@ -426,7 +437,7 @@ With these imports:
426437

427438
```{code-cell} python3
428439
import datetime as dt
429-
from pandas_datareader import data
440+
import yfinance as yf
430441
```
431442

432443
Write a program to calculate the percentage price change over 2019 for the following shares:
@@ -460,7 +471,8 @@ def read_data(ticker_list,
460471
ticker = pd.DataFrame()
461472
462473
for tick in ticker_list:
463-
prices = data.DataReader(tick, 'yahoo', start, end)
474+
stock = yf.Ticker(tick)
475+
prices = stock.history(start=start, end=end)
464476
closing_prices = prices['Close']
465477
ticker[tick] = closing_prices
466478

0 commit comments

Comments
 (0)