import pandas as pd
import numpy as np
Accessing and Managing Financial Data
You are reading the work-in-progress edition of Tidy Finance with Python. Code chunks and text might change over the next couple of months. We are always looking for feedback via contact@tidy-finance.org. Meanwhile, you can find the complete R version here.
In this chapter, we suggest a way to organize your financial data. Everybody, who has experience with data, is also familiar with storing data in various formats like CSV, XLS, XLSX, or other delimited value storage. Reading and saving data can become very cumbersome in the case of using different data formats, both across different projects and across different programming languages. Moreover, storing data in delimited files often leads to problems with respect to column type consistency. For instance, date-type columns frequently lead to inconsistencies across different data formats and programming languages.
This chapter shows how to import different open source data sets. Specifically, our data comes from the application programming interface (API) of Yahoo!Finance, a downloaded standard CSV file, an XLSX file stored in a public Google Drive repository, and other macroeconomic time series. We store all the data in a single database, which serves as the only source of data in subsequent chapters. We conclude the chapter by providing some tips on managing databases.
First, we load the global packages that we use throughout this chapter. Later on, we load more packages in the sections where we need them.
Moreover, we initially define the date range for which we fetch and store the financial data, making future data updates tractable. In case you need another time frame, you can adjust the dates below. Our data starts with 1960 since most asset pricing studies use data from 1962 on.
= "1960-01-01"
start_date = "2021-12-31" end_date
Fama-French Data
We start by downloading some famous Fama-French factors (e.g., Fama and French 1993) and portfolio returns commonly used in empirical asset pricing. Fortunately, the pandas-datareader
package provides a simple interface to read data from Ken French’s Data Library.
import pandas_datareader as pdr
We can use the pdr.DataReader()
function of the package to download monthly Fama-French factors. The set 3 Factors includes the return time series of the market, size, and value factors alongside the risk-free rates. Note that we have to do some manual work to correctly parse all the columns and scale them appropriately, as the raw Fama-French data comes in a very unpractical data format. For precise descriptions of the variables, we suggest consulting Prof. Kenneth French’s finance data library directly. If you are on the site, check the raw data files to appreciate the time you can save thanks to pandas-datareader
.
= pdr.DataReader(name="F-F_Research_Data_Factors",
factors_ff_monthly_raw ="famafrench",
data_source=start_date,
start=end_date)[0]
end
= (factors_ff_monthly_raw
factors_ff_monthly 100)
.divide(="month")
.reset_index(names= lambda x: pd.to_datetime(x["month"].astype(str)))
.assign(month str.lower, axis="columns")
.rename(= {"mkt-rf" : "mkt_excess"})
.rename(columns )
It is straightforward to download the corresponding daily Fama-French factors with the same function.
= pdr.DataReader("F-F_Research_Data_Factors_daily",
factors_ff_daily_raw "famafrench",
start_date, 0]
end_date)[
= (factors_ff_daily_raw
factors_ff_daily 100)
.divide(="date")
.reset_index(namesstr.lower, axis="columns")
.rename(= {"mkt-rf" : "mkt_excess"})
.rename(columns )
In a subsequent chapter, we also use the 10 monthly industry portfolios, so let us fetch that data, too.
= pdr.DataReader("10_Industry_Portfolios",
industries_ff_monthly_raw "famafrench",
start_date, 0]
end_date)[
= (industries_ff_monthly_raw
industries_ff_monthly 100)
.divide(="month")
.reset_index(names= lambda x: pd.to_datetime(x["month"].astype(str)))
.assign(month )
It is worth taking a look at all available portfolio return time series from Kenneth French’s homepage. You should check out the other sets by calling pdr.famafrench.get_available_datasets()
.
q-Factors
In recent years, the academic discourse experienced the rise of alternative factor models, e.g., in the form of the Hou, Xue, and Zhang (2014) q-factor model. We refer to the extended background information provided by the original authors for further information. The q factors can be downloaded directly from the authors’ homepage from within pd.read_csv()
.
We also need to adjust this data. First, we discard information we will not use in the remainder of the book. Then, we rename the columns with the “R_”-prescript using regular expressions and write all column names in lowercase. You should always try sticking to a consistent style for naming objects, which we try to illustrate here - the emphasis is on try. You can check out style guides available online, e.g., Hadley Wickham’s tidyverse
style guide.
= ("http://global-q.org/uploads/1/2/2/6/122679606/" +
factors_q_monthly_link "q5_factors_monthly_2021.csv")
=(pd.read_csv(factors_q_monthly_link)
factors_q_monthly= lambda x: (pd.to_datetime(x["year"].astype(str)
.assign(month + "-" + x["month"].astype(str) + "-01")))
=["R_F", "R_MKT", "year"])
.drop(columns= lambda x: x.replace("R_", "").lower())
.rename(columns "month >= @start_date and month <= @end_date")
.query(**{col: lambda x: x[col] / 100 for col in ["me", "ia", "roe", "eg"]})
.assign( )
Macroeconomic Predictors
Our next data source is a set of macroeconomic variables often used as predictors for the equity premium. Welch and Goyal (2008) comprehensively reexamine the performance of variables suggested by the academic literature to be good predictors of the equity premium. The authors host the data updated to 2021 on Amit Goyal’s website. Since the data is an XLSX-file stored on a public Google drive location, we need additional packages to access the data directly from our Python session. Usually, you need to authenticate if you interact with Google drive directly in Python. Since the data is stored via a public link, we can proceed without any authentication.
= "1OArfD2Wv9IvGoLkJ8JyoXS0YMQLDZfY2"
sheet_id = "macro_predictors.xlsx"
sheet_name = ("https://docs.google.com/spreadsheets/d/" + sheet_id +
macro_predictors_link "/gviz/tq?tqx=out:csv&sheet=" + sheet_name)
Next, we read in the new data and transform the columns into the variables that we later use:
- The dividend price ratio (
dp
), the difference between the log of dividends and the log of prices, where dividends are 12-month moving sums of dividends paid on the S&P 500 index, and prices are monthly averages of daily closing prices (Campbell and Shiller 1988; Campbell and Yogo 2006). - Dividend yield (
dy
), the difference between the log of dividends and the log of lagged prices (Ball 1978). - Earnings price ratio (
ep
), the difference between the log of earnings and the log of prices, where earnings are 12-month moving sums of earnings on the S&P 500 index (Campbell and Shiller 1988). - Dividend payout ratio (
de
), the difference between the log of dividends and the log of earnings (Lamont 1998). - Stock variance (
svar
), the sum of squared daily returns on the S&P 500 index (Guo 2006). - Book-to-market ratio (
bm
), the ratio of book value to market value for the Dow Jones Industrial Average (Kothari and Shanken 1997) - Net equity expansion (
ntis
), the ratio of 12-month moving sums of net issues by NYSE listed stocks divided by the total end-of-year market capitalization of NYSE stocks (Campbell, Hilscher, and Szilagyi 2008). - Treasury bills (
tbl
), the 3-Month Treasury Bill: Secondary Market Rate from the economic research database at the Federal Reserve Bank at St. Louis (Campbell 1987). - Long-term yield (
lty
), the long-term government bond yield from Ibbotson’s Stocks, Bonds, Bills, and Inflation Yearbook (Welch and Goyal 2008). - Long-term rate of returns (
ltr
), the long-term government bond returns from Ibbotson’s Stocks, Bonds, Bills, and Inflation Yearbook (Welch and Goyal 2008). - Term spread (
tms
), the difference between the long-term yield on government bonds and the Treasury bill (Campbell 1987). - Default yield spread (
dfy
), the difference between BAA and AAA-rated corporate bond yields (Fama and French 1989). - Inflation (
infl
), the Consumer Price Index (All Urban Consumers) from the Bureau of Labor Statistics (Campbell and Vuolteenaho 2004).
For variable definitions and the required data transformations, you can consult the material on Amit Goyal’s website.
= (pd.read_csv(macro_predictors_link, thousands=",")
macro_predictors = lambda x: pd.to_datetime(x["yyyymm"], format="%Y%m"),
.assign(month = lambda x: x["Index"] + x["D12"],
IndexDiv = lambda x: (np.log(x["IndexDiv"]) -
logret"IndexDiv"].shift(1))),
np.log(x[= lambda x: np.log(x["Rfree"] + 1),
Rfree = lambda x: x["logret"] - x["Rfree"].shift(-1),
rp_div = lambda x: np.log(x["D12"]) - np.log(x["Index"]),
dp = lambda x: np.log(x["D12"]) - np.log(x["D12"].shift(1)),
dy = lambda x: np.log(x["E12"]) - np.log(x["Index"]),
ep = lambda x: np.log(x["D12"]) - np.log(x["E12"]),
de = lambda x: x["lty"] - x["tbl"],
tms = lambda x: x["BAA"] - x["AAA"])
dfy "month", "rp_div", "dp", "dy", "ep", "de", "svar", "b/m", "ntis",
.get(["tbl", "lty", "ltr", "tms", "dfy", "infl"])
"month >= @start_date and month <= @end_date")
.query(
.dropna() )
Other Macroeconomic Data
The Federal Reserve bank of St. Louis provides the Federal Reserve Economic Data (FRED), an extensive database for macroeconomic data. In total, there are 817,000 US and international time series from 108 different sources. As an illustration, we use the already familiar pandas-datareader
package to fetch consumer price index (CPI) data that can be found under the CPIAUCNS key.
= (pdr.DataReader("CPIAUCNS",
cpi_monthly "fred",
start_date,
end_date)="month")
.reset_index(names= {"CPIAUCNS" : "cpi"})
.rename(columns =lambda x: x["cpi"] / x["cpi"].iloc[-1])
.assign(cpi )
To download other time series, we just have to look it up on the FRED website and extract the corresponding key from the address. For instance, the producer price index for gold ores can be found under the PCU2122212122210 key.
Setting Up a Database
Now that we have downloaded some (freely available) data from the web into the memory of our R session let us set up a database to store that information for future use. We will use the data stored in this database throughout the following chapters, but you could alternatively implement a different strategy and replace the respective code.
There are many ways to set up and organize a database, depending on the use case. For our purpose, the most efficient way is to use an SQLite database, which is the C-language library that implements a small, fast, self-contained, high-reliability, full-featured, SQL database engine. Note that SQL (Structured Query Language) is a standard language for accessing and manipulating databases.
import sqlite3
An SQLite database is easily created - the code below is really all there is. You do not need any external software. Otherwise, date columns are stored and retrieved as integers. We will use the resulting file tidy_finance.db
in the subfolder data
for all subsequent chapters to retrieve our data.
= sqlite3.connect("data/tidy_finance.sqlite") tidy_finance
Next, we create a remote table with the monthly Fama-French factor data. We do so with the function to_sql()
, which copies the data to our SQLite-database. Before we copy the data to the database, we convert the date to UNIX integers, which allows us to smoothly share the data between R and Python. We follow the approach recommended by pandas
for this conversion.
(factors_ff_monthly= lambda x: (x["month"]- pd.Timestamp("1970-01-01")) // pd.Timedelta("1d"))
.assign(month ="factors_ff_monthly",
.to_sql(name=tidy_finance,
con="replace",
if_exists= False)
index )
744
If we want to have the whole table in memory, we need to call pd.read_sql_query()
with the corresponding query. You will see that we regularly load the data into the memory in the next chapters.
(pd.read_sql_query(="SELECT month, rf FROM factors_ff_monthly",
sql=tidy_finance,
con={"month": {"unit": "D", "origin": "unix"}})
parse_dates )
month | rf | |
---|---|---|
0 | 1960-01-01 | 0.0033 |
1 | 1960-02-01 | 0.0029 |
2 | 1960-03-01 | 0.0035 |
3 | 1960-04-01 | 0.0019 |
4 | 1960-05-01 | 0.0027 |
... | ... | ... |
739 | 2021-08-01 | 0.0000 |
740 | 2021-09-01 | 0.0000 |
741 | 2021-10-01 | 0.0000 |
742 | 2021-11-01 | 0.0000 |
743 | 2021-12-01 | 0.0001 |
744 rows × 2 columns
The last couple of code chunks is really all there is to organizing a simple database! You can also share the SQLite database across devices and programming languages.
Before we move on to the next data source, let us also store the other five tables in our new SQLite database.
(factors_ff_daily= lambda x: (x["date"]- pd.Timestamp("1970-01-01")) // pd.Timedelta("1d"))
.assign(date ="factors_ff_daily",
.to_sql(name=tidy_finance,
con="replace",
if_exists= False)
index
)
(industries_ff_monthly= lambda x: (x["month"]- pd.Timestamp("1970-01-01")) // pd.Timedelta("1d"))
.assign(month ="industries_ff_monthly",
.to_sql(name=tidy_finance,
con="replace",
if_exists= False)
index
)
(factors_q_monthly= lambda x: (x["month"]- pd.Timestamp("1970-01-01")) // pd.Timedelta("1d"))
.assign(month ="factors_q_monthly",
.to_sql(name=tidy_finance,
con="replace",
if_exists= False)
index
)
(macro_predictors= lambda x: (x["month"]- pd.Timestamp("1970-01-01")) // pd.Timedelta("1d"))
.assign(month ="macro_predictors",
.to_sql(name=tidy_finance,
con="replace",
if_exists= False)
index
)
(cpi_monthly= lambda x: (x["month"]- pd.Timestamp("1970-01-01")) // pd.Timedelta("1d"))
.assign(month ="cpi_monthly",
.to_sql(name=tidy_finance,
con="replace",
if_exists= False)
index )
From now on, all you need to do to access data that is stored in the database is to follow two steps: (i) Establish the connection to the SQLite database and (ii) execute the query to fetch the data. For your convenience, the following steps show all you need in a compact fashion.
import pandas
import sqlite3
= sqlite3.connect("data/tidy_finance.sqlite")
tidy_finance = (pd.read_sql_query(
factors_q_monthly ="SELECT * FROM factors_q_monthly",
sql=tidy_finance,
con={"month": {"unit": "D", "origin": "unix"}})
parse_dates )
Managing SQLite Databases
Finally, at the end of our data chapter, we revisit the SQLite database itself. When you drop database objects such as tables or delete data from tables, the database file size remains unchanged because SQLite just marks the deleted objects as free and reserves their space for future uses. As a result, the database file always grows in size.
To optimize the database file, you can run the VACUUM
command in the database, which rebuilds the database and frees up unused space. You can execute the command in the database using the execute()
function.
"VACUUM") tidy_finance.execute(
The VACUUM
command actually performs a couple of additional cleaning steps, which you can read up in this tutorial.
Exercises
- Download the monthly Fama-French factors manually from Ken French’s data library and read them in via
pd.read_csv()
. Validate that you get the same data as via thepandas-datareader
package. - Download the Fama-French 5 factors using the
pandas-datareader
package. Usepdr.famafrench.get_available_datasets()
to find the corresponding table name. After the successful download and conversion to the column format that we used above, compare the resultingrf
,mkt_excess
,smb
, andhml
columns tofactors_ff_monthly
. Explain any differences you might find.