import pandas as pd
from fredapi import Fred
import datetime as dt
# set up Fred key else queries do not work
fred = Fred(api_key='yourkeygoeshere')
# Search for the ticker by description
ticker_description = 'Civilian Unemployment Rate'
ticker_info = fred.search(ticker_description)
if ticker_info is None:
print("Did not find ticker by searching the text '{0}'. Pleave revise your search.".format(ticker_description))
else:
ticker_id = ticker_info['id'].values[0]
ticker_start = ticker_info['observation_start'].values[0]
ticker_end = ticker_info['observation_end'].values[0]
print("Ticker id = {0}, start_date = {1}, end_date = {2}".format(ticker_id, ticker_start, ticker_end))
try:
# query the ticker information from FRED using start and end dates
s = fred.get_series(ticker_id, observation_start=ticker_start, observation_end=ticker_end)
print(s.tail())
# Save daily close file since inception
s.to_csv("output/"+ticker_id+".csv")
# Save quarterly file. Use 'QS' frequency for beginning of month data
pd.Series(s, pd.date_range('2013-01-05', dt.datetime.now(), freq='QS')).to_csv("output/"+ticker_id+"_quarterly.csv")
except:
print("Problem downloading '{0}' series".format(ticker_id))
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
s.plot(figsize=(10,5), grid=True, title='{0}% ({1})'.format(ticker_description, ticker_id))