沿時間軸平移股票價格
你在影片中看到的第一個時間序列操作方法是 .shift(),它可以沿著 DateTimeIndex,將 Series 或 DataFrame 的所有值平移指定的期數到不同的時間點。
我們來用它把 Google 的股價序列分別平移 90 個商業日到過去與未來,做視覺化比較。
本練習屬於課程
Manipulating Time Series Data in Python
練習說明
我們已經將 pandas 以 pd 匯入,並匯入 matplotlib.pyplot 為 plt。
- 使用
pd.read_csv()匯入'google.csv',將'Date'解析為日期,並設為index,指定給變數google。 - 使用
.asfreq()將google的頻率設為商業日。 - 在
google中新增lagged與shifted欄,分別放入把Close平移 90 個商業日到過去與未來的結果。 - 繪製
google的三個欄位。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import data here
google = ____
# Set data frequency to business daily
google = ____
# Create 'lagged' and 'shifted'
google['lagged'] = ____
google['shifted'] = ____
# Plot the google price series