Skip to content

Instantly share code, notes, and snippets.

@geekypy
Created June 5, 2021 15:49
Show Gist options
  • Save geekypy/42e03fceb068cb6dd0450ab9003034c2 to your computer and use it in GitHub Desktop.
Save geekypy/42e03fceb068cb6dd0450ab9003034c2 to your computer and use it in GitHub Desktop.
Including a function to extract FRED data filtering by a range of years
#!python3
import sys ,os , requests
import time, json
import matplotlib.pyplot as plt
# This library retrieves and relies on data coming from FRED API
# Attributes
api_key="api_key=2665c3dd755e8d7912000c862a60c365"
def category_search_by():
entry = input("Please introduce keywords: ")
req1 = "https://api.stlouisfed.org/fred/series/search?search_text="+entry+"&"+api_key+"&file_type=json"
req = requests.get(req1)
print(json.loads(req.text)['seriess'])
def data_series_filtered(id, start_yr, end_yr):
# This function is intended to help the user to filter by start/end yr i.e. of certain series
req1 = "https://api.stlouisfed.org/fred/series/observations?series_id=" + id + "&" + api_key + "&file_type=json"
print(req1 + "\n")
req = requests.get(req1)
observations = json.loads(req.text)['observations']
date_s= list()
value_s = list ()
index = 0
while index < len(observations):
year = int(observations[index]["date"][0:4])
if year >= int(start_yr) and year <= int(end_yr):
date = observations[index]["date"]
date_s.append(date)
value = float(observations[index]["value"])
value_s.append(value)
index = index + 1
else:
index = index + 1
print(date_s,value_s)
return date_s, value_s
def data_series_extraction(id):
req1 = "https://api.stlouisfed.org/fred/series/observations?series_id="+id+"&"+api_key+"&file_type=json"
print(req1+"\n")
req = requests.get(req1)
observations =json.loads(req.text)['observations']
date_s = list()
value_s = list()
for i in observations:
month = "-04-"
#Selecting only April months' of each year
if month in i['date']:
date_s.append(i['date'])
value_s.append(float(i['value']))
date_s_s = [*range(0,len(date_s))]
# if data on data_s_s returned instead of date_s (yearly data) then it is intended to avoid date overlapping in the x axis
# when using matplotlib
return date_s_s,value_s
def plot_single(date_s,value_s,x_label,y_label,title_s):
plt.plot(date_s, value_s)
plt.ylabel(y_label)
plt.xlabel(x_label)
plt.title(title_s)
plt.show()
def plot_multiple(d1,v1,d2,v2,title_s,x_label,y_label):
plt.plot(d1, v1)
plt.plot(d2, v2)
plt.ylabel(y_label)
plt.xlabel(x_label)
plt.title(title_s)
plt.show()
def main():
# Example to plot filtered data from FRED repositories
dm1, vm1 = data_series_filtered("MANMM101USA657S", "2000", "2020")
dm2, vm2 = data_series_filtered("MABMM301USA657S", "2000", "2020")
plot_multiple(dm1, vm1, dm2, vm2, "M1 vs. M2 Growth Rate (Previous Period, Not Seasonally Adjusted)","2011-2020","% Growth Rate")
#data_series_extraction("WM2NS","From 1980 t0 2021 (May)","Billions of Dollars","M2 United States (Money Stock)")
#dm2,vm2 = data_series_extraction("M2SL")
#dm1,vm1 = data_series_extraction("M1SL")
#plot_multiple(dm1, vm1, dm2, vm2,"M1 vs. M2 United States (Money Stock)","From 1959 to 2021 (May)", "Billions of Dollars")
#plot_single(dm2,vm2,"From 1959 t0 2021 (May)", "Billions of Dollars", "M2 United States (Money Stock)")
#data_series_extraction("M1SL", "From 1959 t0 2021 (May)", "Billions of Dollars", "M1 United States (Money Stock)")
if __name__ == "__main__":
main()
@geekypy
Copy link
Author

geekypy commented Jun 5, 2021

New Function that aims to offer the ability to filter and extract date from a year range

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment