Blogs

Getting GDP Data from Fred

This is an example for getting data from FRED using Pandas DataReader library. It's free and don't require and api key. Also, an example data analysis with GDP data is provided. 

First get the libraries:

import pandas_datareader as pdr
import datetime as dt
import matplotlib.pyplot as plt

Then, get the data from FRED:

# gdp in billion USD (2012 usd value adjusted)
start = dt.datetime(1950,5,1)
end = dt.datetime(2022,6,30)

gdp = pdr.DataReader(['GDP', 'GDPC1', 'A939RX0Q048SBEA'], 'fred', start, end)
gdp.columns = ['nom_gdp', 'real_gdp', 'real_gdp_capita']

Visualize nominal and real gdp:

gdp.nom_gdp.plot(label='Nominal GDP')
gdp.real_gdp.plot(label='Real GDP')
plt.title('Nominal vs Real GDP (2012 usd adjusted)')
plt.ylabel('GDP')
plt.yscale("log")
plt.legend()
plt.show()

Calculate GDP changes:

gdp['realgdp_change'] = gdp['real_gdp'].pct_change()
gdp['realgdp_capita_change'] = gdp['real_gdp_capita'].pct_change()

Yearly Avg Real GDP Change:  3.0 %
Yearly Avg Real GDP Per Capita Change:  1.9 %

Finally visualize real GDP changes as a distribution:

plt.hist(gdp['realgdp_change']*4*100,bins=50,color='g', alpha=0.7)
plt.title('Real GDP Change Distribution 1950-2022' )
plt.ylabel('Frequency')
plt.xlabel('% Yearly GDP Change')
plt.show()
https://bitlomy.s3.amazonaws.com/media/uploads/2022/07/28/gdp-change.png