從每月失業率建立每週資料
美國民間失業率是以每月發布。你可能需要更高頻率的資料,但沒問題,因為你剛學會如何將時間序列上取樣。
你將使用過去 20 年的時間序列資料,並在繪製每週序列前,套用幾個選項來填補遺漏值。
本練習屬於課程
Manipulating Time Series Data in Python
練習說明
我們已經將 pandas 以 pd 匯入,並將 matplotlib.pyplot 以 plt 匯入。
- 使用
pd.read_csv()匯入'unemployment.csv',把'date'欄位透過parse_dates與index_col轉成DateTimeIndex,並將結果指定給data。 - 使用
.asfreq()與別名'W'將data轉為每週頻率,並顯示前 5 列。 - 再次轉為每週頻率,加入選項
'bfill',並顯示前 5 列。 - 建立每週序列,這次加入選項
'ffill',指定為weekly_ffill,並顯示前 5 列。 - 繪製自 2015 年起的
weekly_ffill。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import data here
data = ____
# Show first five rows of weekly series
print(____)
# Show first five rows of weekly series with bfill option
print(____)
# Create weekly series with ffill option and show first five rows
weekly_ffill = ____
print(____)
# Plot weekly_fill starting 2015 here