import os
import pandas as pd
import seaborn as sns
from full_fred.fred import Fred
# FRED_API_KEY = os.getenv('FRED_API_KEY')
fred = Fred()
print(f"FRED API key is set to an environment variable: {fred.env_api_key_found()}")
def get_fred_data(name, start="2013-01-01", end=""):
df = fred.get_series_df(name)[["date", "value"]].copy()
df["date"] = pd.to_datetime(df["date"])
df["value"] = pd.to_numeric(df["value"], errors="coerce")
df = df.set_index("date")
if end == "":
df = df.loc[f"{start}":]
else:
df = df.loc[f"{start}":f"{end}"]
return df
FRED API key is set to an environment variable:True
Original source:https://fred.stlouisfed.org/series/UNRATE
df_UNRATE = get_fred_data("UNRATE", start="2010-01-01")
df_UNRATE.head(5)
value | |
---|---|
date | |
2010-01-01 | 9.8 |
2010-02-01 | 9.8 |
2010-03-01 | 9.9 |
2010-04-01 | 9.9 |
2010-05-01 | 9.6 |
sns.set(rc={"figure.figsize": (15, 8)})
sns.lineplot(data=df_UNRATE, x="date", y="value")
Source of data:https://fred.stlouisfed.org/series/BAMLH0A0HYM2
df_BAMLH0A0HYM2 = get_fred_data("BAMLH0A0HYM2", start="2010-01-01")
df_BAMLH0A0HYM2.head(5)
value | |
---|---|
date | |
2010-01-01 | NaN |
2010-01-04 | 6.34 |
2010-01-05 | 6.30 |
2010-01-06 | 6.17 |
2010-01-07 | 6.03 |
Source of data:https://fred.stlouisfed.org/series/WILLLRGCAPGR
df_WILLLRGCAPGR = get_fred_data("WILLLRGCAPGR", start="2010-01-01")
df_WILLLRGCAPGR.head(5)
value | |
---|---|
date | |
2010-01-01 | NaN |
2010-01-04 | 26423.03 |
2010-01-05 | 26494.24 |
2010-01-06 | 26473.92 |
2010-01-07 | 26512.36 |
sns.set(rc={"figure.figsize": (15, 8)})
sns.lineplot(data=df_WILLLRGCAPGR, x="date", y="value")
sns.lineplot(data=df_BAMLH0A0HYM2 * 10000, x="date", y="value")