Shifting stock prices across time
The first method to manipulate time series that you saw in the video was .shift(), which allows you shift all values in a Series or DataFrame by a number of periods to a different time along the DateTimeIndex.
Let's use this to visually compare a stock price series for Google shifted 90 business days into both past and future.
This exercise is part of the course
Manipulating Time Series Data in Python
Exercise instructions
We have already imported pandas as pd and matplotlib.pyplot as plt.
- Use
pd.read_csv()to import'google.csv', parsing the'Date'as dates, setting the result asindexand assigning togoogle. - Use
.asfreq()to set the frequency ofgoogleto business daily. - Add new columns
laggedandshiftedtogooglethat contain theCloseshifted by 90 business days into past and future, respectively. - Plot the three columns of
google.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import data here
google = ____
# Set data frequency to business daily
google = ____
# Create 'lagged' and 'shifted'
google['lagged'] = ____
google['shifted'] = ____
# Plot the google price series