檢查數值範圍
回到串流新創的電影資料集。在你把 vote_count 和 budget 轉為更小的型別之前,先檢查它們的最大值,並和較小整數型別的上限做比較。這能判斷使用 32 位元(或 16 位元)是否安全。
本練習屬於課程
使用 Polars 擴充與最佳化資料管線
練習說明
- 計算
vote_count和budget的最大值。 - 顯示將
budget視為Int32與Int16時各自的上限。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
result = movies.select(
# Largest vote_count
pl.col("vote_count").____().alias("vote_count_max"),
# Largest budget
pl.col("budget").____().alias("budget_max"),
# Upper bounds of smaller dtypes
pl.col("budget").cast(pl.Int32).____().alias("budget_int32_upper"),
pl.col("budget").cast(pl.Int16).____().alias("budget_int16_upper"),
)
print(result)