From 791150becf164ade7e785c986d85df4ecbecace7 Mon Sep 17 00:00:00 2001 From: mmcky Date: Tue, 13 Apr 2021 16:12:29 +1000 Subject: [PATCH 1/4] update pandas to use yfinance and minor edits --- lectures/pandas.md | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lectures/pandas.md b/lectures/pandas.md index baf87644..75322366 100644 --- a/lectures/pandas.md +++ b/lectures/pandas.md @@ -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 @@ -385,18 +386,30 @@ 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 ` to Access Data +### Using {index}`pandas_datareader ` and {index}`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. +:::{margin} +```{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. @@ -426,7 +439,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: From 3efeb64b683da81bb4b701c3a8566d2b4bc50d85 Mon Sep 17 00:00:00 2001 From: mmcky Date: Tue, 13 Apr 2021 16:20:12 +1000 Subject: [PATCH 2/4] fix read function for yahoo finance --- lectures/pandas.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lectures/pandas.md b/lectures/pandas.md index 75322366..e1d2434e 100644 --- a/lectures/pandas.md +++ b/lectures/pandas.md @@ -473,7 +473,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 From 7419cd29a99eb46e0e0c8e4ca316f69c815b5a0b Mon Sep 17 00:00:00 2001 From: mmcky Date: Tue, 13 Apr 2021 16:20:52 +1000 Subject: [PATCH 3/4] remove margin as not supported in quantecon-book-theme --- lectures/pandas.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/lectures/pandas.md b/lectures/pandas.md index e1d2434e..febf9870 100644 --- a/lectures/pandas.md +++ b/lectures/pandas.md @@ -403,12 +403,10 @@ in the exercises. For now let's work through one example of downloading and plotting data --- this time from the World Bank. -:::{margin} ```{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. From 5f790e40517f7c9e7f52f4625e21804346051b6d Mon Sep 17 00:00:00 2001 From: mmcky Date: Tue, 13 Apr 2021 16:25:46 +1000 Subject: [PATCH 4/4] add a space for title --- lectures/pandas.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lectures/pandas.md b/lectures/pandas.md index febf9870..0d06e311 100644 --- a/lectures/pandas.md +++ b/lectures/pandas.md @@ -386,7 +386,7 @@ 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 ` and {index}`yfinance `to Access Data +### Using {index}`pandas_datareader ` and {index}`yfinance ` to Access Data ```{index} single: Python; pandas-datareader ```