Visualize monthly mean, median and standard deviation of S&P500 returns
You have also learned how to calculate several aggregate statistics from upsampled data.
Let's use this to explore how the monthly mean, median and standard deviation of daily S&P500 returns have trended over the last 10 years.
Den här övningen är en del av kursen
Manipulating Time Series Data in Python
Övningsinstruktioner
As usual, we have imported pandas as pd and matplotlib.pyplot as plt for you.
- Use
pd.read_csv()to import'sp500.csv', set aDateTimeIndexbased on the'date'column usingparse_datesandindex_col, assign the results tosp500, and inspect using.info(). - Convert
sp500to apd.Series()using.squeeze(), and apply.pct_change()to calculatedaily_returns. .resample()daily_returnsto month-end frequency (alias:'M'), and apply.agg()to calculate'mean','median', and'std'. Assign the result tostats..plot()stats.
Interaktiv övning med praktiskt arbete
Testa den här övningen genom att slutföra den här exempelkoden.
# Import data here
sp500 = ____
# Calculate daily returns here
daily_returns = ____
# Resample and calculate statistics
stats = ____
# Plot stats here