import zipfile
from pathlib import Path
import pandas as pd
from lazymyles import download_file
US Interest Rates
python
united states
financial
Exploration of the Federal Reserve Board H15, US Interest rates dataset.
= Path("data/").resolve()
DATA_PATH
= DATA_PATH / "source/"
SOURCE_PATH = DATA_PATH / "interim/"
INTERIM_PATH = DATA_PATH / "output/"
OUTPUT_PATH
for path in (DATA_PATH, SOURCE_PATH, INTERIM_PATH, OUTPUT_PATH):
=True) path.mkdir(exist_ok
= download_file(
FRB_H15_FILE_PATH ="https://www.federalreserve.gov/datadownload/Output.aspx?rel=H15&filetype=zip",
url=SOURCE_PATH,
output_path="FRB_H15.zip",
file_name
)
with zipfile.ZipFile(FRB_H15_FILE_PATH, "r") as zip_file_obj:
/ "FRB_H15/") zip_file_obj.extractall(SOURCE_PATH
= pd.read_xml(
df / "FRB_H15/H15_data.xml",
SOURCE_PATH =".//kf:Series[@SERIES_NAME='RIFSPFF_N.B']/frb:Obs",
xpath={
namespaces"frb": "http://www.federalreserve.gov/structure/compact/common",
"kf": "http://www.federalreserve.gov/structure/compact/H15_H15",
},=True,
attrs_only
) df.head()
OBS_STATUS | OBS_VALUE | TIME_PERIOD | |
---|---|---|---|
0 | A | 1.13 | 1954-07-01 |
1 | A | 1.25 | 1954-07-02 |
2 | A | 0.88 | 1954-07-05 |
3 | A | 0.25 | 1954-07-06 |
4 | A | 1.00 | 1954-07-07 |
= df[df["OBS_VALUE"] > -9998.9]
df df.head()
OBS_STATUS | OBS_VALUE | TIME_PERIOD | |
---|---|---|---|
0 | A | 1.13 | 1954-07-01 |
1 | A | 1.25 | 1954-07-02 |
2 | A | 0.88 | 1954-07-05 |
3 | A | 0.25 | 1954-07-06 |
4 | A | 1.00 | 1954-07-07 |
/ "us-interest-rates.csv", index=False) df.to_csv(INTERIM_PATH
df.rename(={
columns"OBS_STATUS": "status",
"OBS_VALUE": "value",
"TIME_PERIOD": "date",
},=True,
inplace )
= df.plot(
plot ="date",
x="value",
y="area",
kind=(20, 10),
figsize="US Interest Rate",
title=True,
grid=False,
legend="Date",
xlabel="Rate",
ylabel=15,
fontsize
)/ "plot.png") plot.get_figure().savefig(OUTPUT_PATH
Made by Myles Braithwaite with ❤️ in Toronto.