检查数值范围
回到这家流媒体初创公司的电影数据集。在将 vote_count 和 budget 降级转换为更小的 dtype 之前,请先检查它们的最大值,并与更小整数类型的上界进行比较。这样可以判断使用 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)