開始使用免費開始

回到未來

資料管線的最新更新已將每次搭乘的日期寫入 ride_sharing DataFrame。這個資訊存放在 ride_date 欄,型別是 object,在 pandas 中表示字串。

我們發現一個臭蟲,會把今天的搭乘誤記為明年。為了修正,你需要找出 ride_date 欄中所有在未來的日期,並將此欄位可能的最大值設為今天的日期。執行前,你需要先把 ride_date 轉成 datetime 物件。

datetime 套件已經以 dt 匯入,另外你先前使用的所有套件也都可用。

本練習屬於課程

用 Python 進行資料清理

檢視課程

練習說明

  • 使用 to_datetime()ride_date 轉為 datetime 物件,接著把這個 datetime 物件轉成 date,並存到 ride_dt 欄位。
  • 建立變數 today,使用 dt.date.today() 函式來儲存今天的日期。
  • 將所有未來的 ride_dt 值設為今天的日期。
  • 列印 ride_dt 欄位中的最大日期。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Convert ride_date to date
ride_sharing['ride_dt'] = pd.____(____['____']).dt.date

# Save today's date
today = ____

# Set all in the future to today's date
ride_sharing.____[____['____'] > ____, '____'] = ____

# Print maximum of ride_dt column
print(ride_sharing['ride_dt'].____())
編輯並執行程式碼