Compare quarterly GDP growth rate and stock returns
With your new skill to downsample and aggregate time series, you can compare higher-frequency stock price series to lower-frequency economic time series.
As a first example, let's compare the quarterly GDP growth rate to the quarterly rate of return on the (resampled) Dow Jones Industrial index of 30 large US stocks.
GDP growth is reported at the beginning of each quarter for the previous quarter. To calculate matching stock returns, you'll resample the stock index to quarter start frequency using the alias 'QS'
, and aggregating using the .first()
observations.
This exercise is part of the course
Manipulating Time Series Data in Python
Exercise instructions
As usual, we have imported pandas
as pd
and matplotlib.pyplot
as plt
for you.
- Use
pd.read_csv()
to import'gdp_growth.csv'
and'djia.csv'
, for both set aDateTimeIndex
based on the'date'
column usingparse_dates
andindex_col
, and assign the results togdp_growth
anddjia
respectively, then inspect using.info()
. - Resample
djia
using frequency alias'QS'
, aggregate using.first()
, and assign todjia_quarterly
. - Apply
.pct_change()
todjia_quarterly
and.mul()
by 100 to obtaindjia_quarterly_return
. - Use
pd.concat()
to concatenategdp_growth
anddjia_quarterly_return
alongaxis=1
, and assign todata
. Rename the columns using.columns
and the new labels'gdp'
and'djia'
, then.plot()
the results.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import and inspect gdp_growth here
gdp_growth = ____
# Import and inspect djia here
djia = ____
# Calculate djia quarterly returns here
djia_quarterly = ____
djia_quarterly_return = ____
# Concatenate, rename and plot djia_quarterly_return and gdp_growth here
data = ____