在已排序数据上进行快速路径过滤
历史分析团队需要获取 2021 年之前的所有借阅记录。该 CSV 已按 date 排序,因此只要告诉 Polars 这一点,它在遇到首个 2021 年的行时就能停止扫描。
本练习是课程的一部分
使用 Polars 扩展与优化数据流水线
练习说明
- 将
date列标记为已排序,以便 Polars 使用快速路径扫描。 - 仅保留
date早于 2021 年 1 月 1 日的行。 - 执行惰性查询。
交互式实操练习
通过完成这段示例代码来试试这个练习。
result = (
library
# Mark date as sorted to enable the fast-path scan
.____("date")
# Filter to rows before 2021-01-01
.filter(pl.col("date") < pl.____(2021, 1, 1))
# Execute the lazy query
.____()
)
print(result.head())