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.
Diese Übung ist Teil des Kurses
Manipulating Time Series Data in Python
Anleitung zur Übung
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 asindex
and assigning togoogle
. - Use
.asfreq()
to set the frequency ofgoogle
to business daily. - Add new columns
lagged
andshifted
togoogle
that contain theClose
shifted by 90 business days into past and future, respectively. - Plot the three columns of
google
.
Interaktive Übung
Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.
# Import data here
google = ____
# Set data frequency to business daily
google = ____
# Create 'lagged' and 'shifted'
google['lagged'] = ____
google['shifted'] = ____
# Plot the google price series