import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pandas_datareader as pdr
import yfinance as yfMcKinney Chapter 8 - Practice - Blank
FINA 6333 for Spring 2025
%precision 4
pd.options.display.float_format = '{:.4f}'.format
# %config InlineBackend.figure_format = 'retina'Announcements
Five-Minute Review
Practice
Download data from Yahoo! Finance for BAC, C, GS, JPM, MS, and PNC and assign to data frame stocks_wide.
Reshape stocks_wide from wide to long with dates and tickers as row indexes and assign to data frame stocks_long.
Add daily returns to both stocks_wide and stocks_long under the name Returns.
Hint: Use pd.MultiIndex() to create a multi index for the wide data frame stocks_wide.
Download the daily benchmark return factors from Ken French’s data library.
Hint: Use the DataReader() function in the pandas-datareader package. We imported this package above with the pdr. prefix.
Add the daily benchmark return factors to stocks_wide and stocks_long.
Write a function download() that accepts tickers and returns a wide data frame of returns with the daily benchmark return factors.
We can even add a shape argument to return a wide or long data frame!
Combine earnings with the returns from stocks_long.
Use the .earnings_dates method described here. Use pd.concat() to combine the result of each the .earnings_date data frames and assign them to a new data frame earnings. Name the row indexes Ticker and Date and swap to match the order of the row index in stocks_long.
Plot the relation between daily returns and earnings surprises
Repeat the earnings exercise with the S&P 100 stocks
With more data, we can more clearly see the positive relation between earnings surprises and returns!