FRED data with requests¶
The FRED API is so easy to use that finding what series to read is the hard part. There are hundreds of thousands of series, too many even to list in a simple html table. API calls through the requests module can help.
For pulling individual series, we do not need an API key, so it's simple for anyone to copy a notebook and follow along. For pulling metadata, a key is required. There is a key in the notebook.
So many FRED sequences have non-farm descriptions that it made me wonder what series they have for farms, so we take a look at that.
Imports¶
# imports
import pandas as pd
import requests
import json
# CONST
fredKey = "see notebook" # api key for notebook
requests.get¶
The requests.get method takes a string argument. The string has the following components connected with anpersands
- base tags search url and tag to look for
- api key
- file_type=json
The decode method for the content attribute from that output is a json string we'll interrogate.
reqS = "&".join(("""https://api.stlouisfed.org/fred/tags/series?tag_names=%s
api_key=%s
file_type=json""" %("farm", fredKey)).split("\n"))
respS = requests.get(reqS).content.decode()
len(respS)
The string from the request can be sent to json.loads to obtain a dictionary.
tD = json.loads(respS)
tD.keys()
It's the "seriess" entry we are interested in, and that is a list of dictionaires.
seriesL = tD["seriess"]
len(seriesL)
So there are over 800 series with "farm" in the tags. Just browsing one shows a dictionary.
seriesL[0]