MulaiMulai sekarang secara gratis

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.

Latihan ini adalah bagian dari kursus

Manipulating Time Series Data in Python

Lihat Kursus

Petunjuk latihan

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.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# Import data here
google = ____

# Set data frequency to business daily
google = ____

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

# Plot the google price series


Edit dan Jalankan Kode