Skip to content

GDP and Population Changes Since 2017

Has the pattern of GDP and US population changed at all between the periods of 2017-2020 and 2021-present? Let's look and see...

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
gdp = pd.read_excel('US_GDP.xlsx', sheet_name='gdp')
change = pd.read_excel('US_GDP.xlsx', sheet_name='change')
population = pd.read_excel('US_GDP.xlsx', sheet_name='population')
annual = pd.read_excel('US_GDP.xlsx', sheet_name='annual_gdp')
Spinner
DataFrameas
df_gdp
variable
SELECT
	(year || ' ' || quarter) AS quarter,
	gdp
FROM gdp

Absolute GDP Growth

with sns.axes_style(style='darkgrid'):
    fig, ax = plt.subplots(figsize=(10,6))
    sns.barplot(data=df_gdp, x='quarter', y='gdp')
    plt.title('Quarterly GDP 2017 to Present')
    ax.spines[['top', 'right']].set_visible(False)
    ax.set_xticklabels(df_gdp['quarter'], rotation=60)
    ax.axvline(x=15.5, color='r')
plt.show()

The plot above makes it look like 2021 to present has a steeper GDP growth rate. Let's see if we can confirm that...

Change in GDP Growth

Spinner
DataFrameas
df_change
variable
SELECT 
	year || ' ' || quarter AS quarter,
	gdp AS change_gdp
FROM change
df_change.info()
with sns.axes_style(style='darkgrid'):
    fig, ax = plt.subplots(figsize=(10,6))
    bars = sns.barplot(data=df_change, x='quarter', y='change_gdp', ax=ax)
    plt.title('Quarterly Percent Change in GDP 2017 to Present')
    ax.spines[['top', 'right']].set_visible(False)
    ax.set_xticklabels(df_gdp['quarter'], rotation=60)
    ax.axvline(x=15.5, color='r')
    for bar in bars.patches:
        yval = bar.get_height()
        plt.text(bar.get_x() + bar.get_width() / 2, yval + 0.01, round(yval, 2), ha='center', va='bottom', mouseover=True)
plt.show()

The Covid pandemic threw a huge wrench into the dataset with the giant drop followed by recovery in 2020. Although it still looks like 2021 to present has higher growth overall. I'll calculate the compound annual growth rate of each period so we can see a definitive value...

# define a cagr function
def cagr(begin_value, end_value, n):
    return (end_value / begin_value) ** (1/n) - 1

# retrieve the gdp values 
value_2017 = df_gdp.loc[df_gdp['quarter'] == '2017 Q1', 'gdp'].values[0]
value_2020 = df_gdp.loc[df_gdp['quarter'] == '2020 Q4', 'gdp'].values[0]
value_2021 = df_gdp.loc[df_gdp['quarter'] == '2021 Q1', 'gdp'].values[0]
value_2023 = df_gdp.loc[df_gdp['quarter'] == '2023 Q4', 'gdp'].values[0]

# apply the function to the 2 periods and print the results
cagr_1 = cagr(value_2017, value_2020, 16)
cagr_2 = cagr(value_2021, value_2023, 12)
print(f"The CAGR for 2017-2020 is {cagr_1 * 100:.2f}%")
print(f"The CAGR for 2021-2023 is {cagr_2 * 100:.2f}%")
gdp_cagr = pd.DataFrame({'period': ['2017-2020', '2021-2023'],
                        'cagr': [0.84, 1.79]})
gdp_cagr
with sns.axes_style(style='darkgrid'):
    fig, ax = plt.subplots(figsize=(10,6))
    bars = sns.barplot(data=gdp_cagr, x='period', y='cagr')
    plt.title('GDP CAGR Comparison Between Time Periods')
    ax.spines[['top', 'right']].set_visible(False)
    for bar in bars.patches:
        yval = bar.get_height()
        plt.text(bar.get_x() + bar.get_width() / 2, yval - 0.1, round(yval, 2), ha='center', va='top')
plt.show()

2021-2023 has over a double CAGR than the 2017-2020 period.