Skip to content

Instantly share code, notes, and snippets.

@geekypy
Last active June 7, 2021 04:41
Show Gist options
  • Save geekypy/23d4d757322896546b181cbb04a0498d to your computer and use it in GitHub Desktop.
Save geekypy/23d4d757322896546b181cbb04a0498d to your computer and use it in GitHub Desktop.
#!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="your key"
def obtain_observations(id):
#This function merely returns the list that contains the observations dictionaries
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:
date_s.append(i['date'])
value_s.append(i['value'])
return observations, date_s, value_s
def accommodate(d1,id):
#Generate two lists containing the observations from a data series matching a list of dates.
#It returns the date and value series accommodated to an upon given date list
obs,date_s,value_s = obtain_observations(id)
date_s_s = list()
value_s_s = list()
#generate the list of matching indexes
check = 0
try:
for index in d1:
for index_1 in date_s:
if str(index) == str(index_1):
value_s_s.append(value_s[check])
date_s_s.append(index)
print("d1 date "+index)
print("date_s "+index_1)
print("value_s "+value_s[check])
check = check + 1
else:
continue
except ValueError:
print("not in list, continue")
intersect_d = list(set(d1)&set(date_s_s))
print(intersect_d,date_s_s,value_s_s)
return date_s_s,value_s_s
def category_search_by():
#Function that returns the category within the data repository when entering specific keywords
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 = "-0"+str(month)+"-"
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 7, 2021

Two new functions added to be used such as observations that consists of accessing the FRED api series and return the observation list and the date,value series extracted from the self dictionary. Besides an optional function called accommodate, that as input of a list of dates is able to return the series values for the input date. The latter is especially useful when analyzing series that do not match in length

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