對已排序資料進行快速篩選
歷史分析團隊需要所有 2021 年之前的結帳紀錄。這個 CSV 已依 date 排序,如果你告訴 Polars 這件事,它在遇到第一筆 2021 年的列時就能停止掃描。
本練習屬於課程
使用 Polars 擴充與最佳化資料管線
練習說明
- 將
date欄位標記為已排序,讓 Polars 可以使用快速掃描路徑。 - 篩選出
date早於 2021 年 1 月 1 日的列。 - 執行這個 lazy 查詢。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
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())