Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Created November 25, 2017 18:37
Show Gist options
  • Save vlad-bezden/cc0eb7aee25794a312a619f9a45f6113 to your computer and use it in GitHub Desktop.
Save vlad-bezden/cc0eb7aee25794a312a619f9a45f6113 to your computer and use it in GitHub Desktop.
Calculate monthly and quarterly Apple (aapl) security returns
import pandas_datareader as pdr
import datetime
aapl = pdr.get_data_yahoo('AAPL',
start=datetime.datetime(2007, 1, 1),
end=datetime.datetime(2018, 1, 1))
# resample 'aapl' to business months, take last observation
monthly = aapl.resample('BM').apply(lambda x: x[-1])
# calculate the monthly percentage change
print(monthly.Close.pct_change())
# resample 'aapl' to quarters, take the mean as value per quarter
quarter = aapl.resample('4M').mean()
# calculate the quarterly percentage change
print(quarter.Close.pct_change())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment