Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Manipulating Time Series Data in Python

Cursus bekijken

Oefeninstructies

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 as index and assigning to google.
  • Use .asfreq() to set the frequency of google to business daily.
  • Add new columns lagged and shifted to google that contain the Close shifted by 90 business days into past and future, respectively.
  • Plot the three columns of google.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Import data here
google = ____

# Set data frequency to business daily
google = ____

# Create 'lagged' and 'shifted'
google['lagged'] = ____
google['shifted'] = ____

# Plot the google price series


Code bewerken en uitvoeren