Created
November 25, 2017 18:37
-
-
Save vlad-bezden/cc0eb7aee25794a312a619f9a45f6113 to your computer and use it in GitHub Desktop.
Calculate monthly and quarterly Apple (aapl) security returns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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