高效查詢執行

使用 Polars 擴充與最佳化資料管線

Liam Brannigan

Data Scientist & Polars Contributor

認識你的講師

$$

$$

  • Liam Brannigan,首席資料科學家
  • ML 與資料工程專家
  • Polars 貢獻者

本課講師 Liam Brannigan 的照片

使用 Polars 擴充與最佳化資料管線

從筆電到雲端

Polars 管線從筆電擴展到雲端

使用 Polars 擴充與最佳化資料管線

這門課適合你嗎?

Polars 課程頁面

  • scan_csv 建立延遲查詢
  • filterselectgroup_by 撰寫運算式
使用 Polars 擴充與最佳化資料管線

第 1 章——最佳化

查詢最佳化

使用 Polars 擴充與最佳化資料管線

第 1 章——最佳化

查詢最佳化

使用 Polars 擴充與最佳化資料管線

第 2 章——高效 I/O

Polars 匯入多種檔案類型

使用 Polars 擴充與最佳化資料管線

第 3 章——更豐富的資料型別

巢狀資料轉成表格的示意圖。

使用 Polars 擴充與最佳化資料管線

第 4 章——管線擴展

串流大型資料表的圖像。

使用 Polars 擴充與最佳化資料管線

芝加哥需求資料集

芝加哥市長資料分析團隊

使用 Polars 擴充與最佳化資料管線

檢視資料集

requests = pl.scan_csv("311_Service_Requests.csv",try_parse_dates=True)

超大型資料集,紀錄芝加哥市民的每筆服務需求

使用 Polars 擴充與最佳化資料管線

檢視資料集

requests.collect()
使用 Polars 擴充與最佳化資料管線

檢視資料集

requests.collect().head(5)
shape: (5, 39)
| TYPE                          | STATUS    | DEPARTMENT     | CREATED_DATE        | ... |
| ---                           | ---       | ---            | ---                 | --- |
| str                           | str       | str            | str                 | ... |
|-------------------------------|-----------|----------------|---------------------|-----|
| Pothole in Street Complaint   | Completed | Transportation | 2019-12-16T10:09:08 | ... |
| Tree Trim Request             | Cancelled | Sanitation     | 2019-09-18T01:05:08 | ... |
| Garbage Cart Maintenance      | Completed | Sanitation     | 2021-01-24T09:14:58 | ... |
| Pothole in Street Complaint   | Completed | Transportation | 2019-03-21T10:41:01 | ... |
| Recycling Pick Up             | Completed | Sanitation     | 2021-02-16T08:28:59 | ... |
使用 Polars 擴充與最佳化資料管線

限制處理筆數

requests.head(5)
使用 Polars 擴充與最佳化資料管線

限制處理筆數

requests.head(5).collect()
shape: (5, 39)
| TYPE                            | STATUS    | DEPARTMENT     | CREATED_DATE        | ... |
| ---                             | ---       | ---            | ---                 | --- |
| str                             | str       | str            | str                 | ... |
|---------------------------------|-----------|----------------|---------------------|-----|
| Pothole in Street Complaint     | Completed | Transportation | 2019-12-16T10:09:08 | ... |
| Tree Trim Request | Completed | Sanitation     | 2019-09-18T01:05:08 | ... |
| Garbage Cart Maintenance        | Completed | Sanitation     | 2021-01-24T09:14:58 | ... |
| Pothole in Street Complaint     | Completed | Transportation | 2019-03-21T10:41:01 | ... |
| Recycling Pick Up               | Completed | Sanitation     | 2021-02-16T08:28:59 | ... |
  • 延後呼叫 collect
使用 Polars 擴充與最佳化資料管線

部門摘要查詢

completed_by_department


使用 Polars 擴充與最佳化資料管線

部門摘要查詢

completed_by_department = requests


使用 Polars 擴充與最佳化資料管線

部門摘要查詢

completed_by_department = requests.filter(
    pl.col("STATUS") == "Completed"
)
使用 Polars 擴充與最佳化資料管線

部門摘要查詢

completed_by_department = requests.filter(
    pl.col("STATUS") == "Completed"
).collect()
使用 Polars 擴充與最佳化資料管線

部門摘要查詢

completed_by_department = requests.filter(
    pl.col("STATUS") == "Completed"
).collect().group_by("DEPARTMENT").len()
使用 Polars 擴充與最佳化資料管線

部門摘要查詢

completed_by_department = requests.filter(
    pl.col("STATUS") == "Completed"
).group_by("DEPARTMENT").len().collect()
shape: (10, 2)
| DEPARTMENT                     | len     |
| ---                            | ---     |
| str                            | u32     |
|-------------------------------|---------|
| 311 City Services              | 4859161 |
| Sanitation                     | 3406631 |
| Aviation                       | 2337842 |
| CDOT - Department of Transport | 1468240 |
使用 Polars 擴充與最佳化資料管線

分歧查詢分支

completed_by_department = requests.filter(
    pl.col("STATUS") == "Completed"
).group_by("DEPARTMENT").len()
completed_by_month = requests.filter(
    pl.col("STATUS") == "Completed"
).group_by("MONTH").len()
使用 Polars 擴充與最佳化資料管線

團隊目前的做法

completed_by_department.collect()
completed_by_month.collect()
使用 Polars 擴充與最佳化資料管線

執行分歧查詢

results = pl.collect_all(


)
使用 Polars 擴充與最佳化資料管線

執行分歧查詢

results = pl.collect_all([
    completed_by_department,
    completed_by_month,
])
使用 Polars 擴充與最佳化資料管線

執行分歧查詢

results[0]  # completed_by_department
shape: (10, 2)
| DEPARTMENT           | len     |
| ---                  | ---     |
| str                  | u32     |
|----------------------|---------|
| 311 City Services    | 4859161 |
| Sanitation           | 3406631 |
| Aviation             | 2337842 |
| Transport            | 1468240 |
results[1]  # completed_by_month
shape: (12, 2)
| MONTH | len  |
| ---   | ---  |
| i64   | u32  |
|-------|------|
| 1     | 2506 |
| 2     | 4566 |
| 3     | 2739 |
| 4     | 2922 |
使用 Polars 擴充與最佳化資料管線

一起來練習吧!

使用 Polars 擴充與最佳化資料管線

Preparing Video For Download...