使用插值建立每週就業資料
你先前使用過美國平民失業率,並以簡單的 forward 或 backfill 方法把月資料轉為週資料。
把你之前的作法,和你在本影片學到的 .interpolate() 方法做比較。
本練習屬於課程
Manipulating Time Series Data in Python
練習說明
我們已替你匯入 pandas 為 pd,以及 matplotlib.pyplot 為 plt。我們也已將 2010 到 2016 年的月失業率載入到變數 monthly。
- 使用
.info()檢視monthly。 - 使用
monthly的index的.min()與.max()作為start與end,建立每週日期的pd.date_range(),並將結果指定給weekly_dates。 - 對
monthly套用.reindex()並使用weekly_dates,將輸出指定給weekly。 - 對
weekly.UNRATE分別套用.ffill()與.interpolate(),建立新欄位'ffill'與'interpolated'。 - 繪製
weekly的圖表。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Inspect data here
print(____)
# Create weekly dates
weekly_dates = ____
# Reindex monthly to weekly data
weekly = ____
# Create ffill and interpolated columns
weekly['ffill'] = ____
weekly['interpolated'] = ____
# Plot weekly