高效查询执行

使用 Polars 扩展与优化数据流水线

Liam Brannigan

Data Scientist & Polars Contributor

认识您的讲师

$$

$$

  • Liam Brannigan,首席数据科学家
  • 机器学习与数据工程专家
  • 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 扩展与优化数据流水线

¡Vamos a practicar!

使用 Polars 扩展与优化数据流水线

Preparing Video For Download...