Step 0: imports and installs
pip install fredapi -q
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import plotly.express as px
import os
plt.style.use('fivethirtyeight')
pd.set_option('max_columns', 500)
color_pal = plt.rcParams['axes.prop_cycle'].by_key()['color']
from fredapi import Fred
fred_key = os.environ["FRED"]
Step 1: Create the fred object
fred = Fred(api_key = fred_key)
Step 2: Search for economic data
sp_search = fred.search('S&P', order_by = 'popularity')
sp_search.head()
Step 3: Pull raw data and plot
sp500 = fred.get_series(series_id = 'SP500')
sp500.plot(figsize = (10,5), title = 'S&P 500', lw = 2)
Step 4: Pull and join multiple data series
uemp_results = fred.search('unemployment')
unrate = fred.get_series('UNRATE')
unrate.plot()
uemp_df = fred.search('unemployment rate state', filter=('frequency', 'Monthly'))
uemp_df
uemp_df = uemp_df.query('seasonal_adjustment == "Seasonally Adjusted" and units == "Percent" and observation_start == "1976-01-01 00:00:00"')
uemp_df
uemp_df = uemp_df.loc[uemp_df['title'].str.contains('Unemployment Rate')]
uemp_df
all_results = []
for myid in uemp_df.index:
results = fred.get_series(myid)
results = results.to_frame(name=myid)
all_results.append(results)
uemp_states = pd.concat(all_results, axis=1).drop(['CSOUUR', 'CMWRUR', 'CWSTUR', 'CNERUR', 'CNEWUR'], axis=1)
uemp_states = uemp_states.dropna()
#plot states uemployment rate
px.line(uemp_states)
Step 5: Pull April 2020 unemployment rate
ax= uemp_states.loc[uemp_states.index == '2020-05-01'].T.sort_values('2020-05-01').plot(kind='barh', figsize=(8,12), width=0.7, edgecolor='black', title='Unemployment rate by state, april May 2020')
ax.legend().remove()
plt.show()
id_to_state = uemp_df['title'].str.replace('Unemployment Rate in ','').to_dict()
uemp_states.columns = [id_to_state[c] for c in uemp_states.columns]
Step 6: Pull participation rate
part_df = fred.search('participation rate by state', filter=('frequency', 'Monthly'))
part_df = part_df.query('seasonal_adjustment == "Seasonally Adjusted" and units == "Percent" and observation_start == "1976-01-01 00:00:00"')
part_df
part_id_to_state = part_df['title'].str.replace('Labor Force Participation Rate for ', '').to_dict()
all_results = []
for myid in part_df.index:
results = fred.get_series(myid)
results = results.to_frame(name=myid)
all_results.append(results)
time.sleep(0.1) # Don't request too fast and get blocked
part_states = pd.concat(all_results, axis=1)
part_states.columns = [part_id_to_state[c] for c in part_states.columns]
part_states
Step 7: Plot Unemployment vs Participation
fig, axs = plt.subplots(10, 5, figsize=(30, 30), sharex=True)
axs = axs.flatten()
i = 0
for state in uemp_states.columns:
if state in ["District Of Columbia","Puerto Rico"]:
continue
ax2 = axs[i].twinx()
uemp_states.query('index >= 2020 and index < 2022')[state] \
.plot(ax=axs[i], label='Unemployment')
part_states.query('index >= 2020 and index < 2022')[state] \
.plot(ax=ax2, label='Participation', color=color_pal[1])
ax2.grid(False)
axs[i].set_title(state)
i += 1
plt.tight_layout()
plt.show()
#Fix DC column
uemp_states = uemp_states.rename(columns={'the District of Columbia' : 'District of Columbia'})
state = 'California'
fig, ax = plt.subplots(figsize=(10, 5), sharex=True)
ax2 = ax.twinx()
uemp_states2 = uemp_states.asfreq('MS')
l1 = uemp_states2.query('index >= 2020 and index < 2022')[state] \
.plot(ax=ax, label='Unemployment')
l2 = part_states.dropna().query('index >= 2020 and index < 2022')[state] \
.plot(ax=ax2, label='Participation', color=color_pal[1])
ax2.grid(False)
ax.set_title(state)
fig.legend(labels=['Unemployment','Participation'])
plt.show()