polars
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePolars Fast DataFrame Library
Polars 快速DataFrame库
Lightning-fast DataFrame library with lazy evaluation and parallel execution.
具备延迟计算和并行执行能力的超高速DataFrame库。
When to Use
适用场景
- Pandas is too slow for your dataset
- Working with 1-100GB datasets that fit in RAM
- Need lazy evaluation for query optimization
- Building ETL pipelines
- Want parallel execution without extra config
- Pandas处理你的数据集速度过慢
- 处理1-100GB且可放入内存的数据集
- 需要通过延迟计算优化查询
- 构建ETL管道
- 无需额外配置即可实现并行执行
Lazy vs Eager Evaluation
延迟计算 vs 即时计算
| Mode | Function | Executes | Use Case |
|---|---|---|---|
| Eager | | Immediately | Small data, exploration |
| Lazy | | On | Large data, pipelines |
Key concept: Lazy mode builds a query plan that gets optimized before execution. The optimizer applies predicate pushdown (filter early) and projection pushdown (select columns early).
| 模式 | 函数 | 执行时机 | 适用场景 |
|---|---|---|---|
| 即时 | | 立即执行 | 小型数据、数据探索 |
| 延迟 | | 调用 | 大型数据、数据管道 |
核心概念:延迟模式会先构建查询计划,在执行前完成优化。优化器会应用谓词下推(提前过滤)和投影下推(提前选择列)。
Core Operations
核心操作
Data Selection
数据选择
| Operation | Purpose |
|---|---|
| Choose columns |
| Choose rows by condition |
| Add/modify columns |
| Remove columns |
| First/last n rows |
| 操作 | 用途 |
|---|---|
| 选择列 |
| 按条件筛选行 |
| 添加/修改列 |
| 删除列 |
| 获取前n行/后n行 |
Aggregation
聚合操作
| Operation | Purpose |
|---|---|
| Group and aggregate |
| Reshape wide |
| Reshape long |
| Distinct values |
| 操作 | 用途 |
|---|---|
| 分组并聚合 |
| 宽表转换 |
| 长表转换 |
| 获取去重值 |
Joins
连接操作
| Join Type | Description |
|---|---|
| inner | Matching rows only |
| left | All left + matching right |
| outer | All rows from both |
| cross | Cartesian product |
| semi | Left rows with match |
| anti | Left rows without match |
| 连接类型 | 说明 |
|---|---|
| inner | 仅保留匹配的行 |
| left | 保留左表所有行及右表匹配行 |
| outer | 保留两张表的所有行 |
| cross | 笛卡尔积连接 |
| semi | 保留左表中存在匹配的行 |
| anti | 保留左表中无匹配的行 |
Expression API
表达式API
Key concept: Polars uses expressions () instead of indexing. Expressions are lazily evaluated and optimized.
pl.col()核心概念:Polars使用表达式()而非索引方式。表达式会延迟计算并进行优化。
pl.col()Common Expressions
常用表达式
| Expression | Purpose |
|---|---|
| Reference column |
| Literal value |
| All columns |
| All except |
| 表达式 | 用途 |
|---|---|
| 引用列 |
| 字面量值 |
| 所有列 |
| 排除指定列外的所有列 |
Expression Methods
表达式方法
| Category | Methods |
|---|---|
| Aggregation | |
| String | |
| DateTime | |
| Conditional | |
| Window | |
| 类别 | 方法 |
|---|---|
| 聚合 | |
| 字符串 | |
| 日期时间 | |
| 条件判断 | |
| 窗口函数 | |
Pandas Migration
Pandas迁移指南
| Pandas | Polars |
|---|---|
| |
| |
| |
| |
| |
Key concept: Polars prefers explicit operations over implicit indexing. Use to name computed columns.
.alias()| Pandas | Polars |
|---|---|
| |
| |
| |
| |
| |
核心概念:Polars更倾向于显式操作而非隐式索引。使用为计算列命名。
.alias()File I/O
文件读写
| Format | Read | Write | Notes |
|---|---|---|---|
| CSV | | | Human readable |
| Parquet | | | Fast, compressed |
| JSON | | | Newline-delimited |
| IPC/Arrow | | | Zero-copy |
Key concept: Use Parquet for performance. Use for large files to enable lazy optimization.
scan_*| 格式 | 读取 | 写入 | 说明 |
|---|---|---|---|
| CSV | | | 人类可读 |
| Parquet | | | 高速、压缩存储 |
| JSON | | | 换行分隔格式 |
| IPC/Arrow | | | 零拷贝 |
核心概念:追求性能时使用Parquet格式。处理大文件时使用方法以启用延迟优化。
scan_*Performance Tips
性能优化技巧
| Tip | Why |
|---|---|
| Use lazy mode | Query optimization |
| Use Parquet | Column-oriented, compressed |
| Select columns early | Projection pushdown |
| Filter early | Predicate pushdown |
| Avoid Python UDFs | Breaks parallelism |
| Use expressions | Vectorized operations |
| Set dtypes on read | Avoid inference overhead |
| 技巧 | 原因 |
|---|---|
| 使用延迟模式 | 实现查询优化 |
| 使用Parquet格式 | 列存储、压缩特性 |
| 提前选择列 | 投影下推优化 |
| 提前过滤数据 | 谓词下推优化 |
| 避免Python自定义函数(UDF) | 会破坏并行执行 |
| 使用表达式 | 矢量化操作 |
| 读取时指定数据类型 | 避免类型推断开销 |
vs Alternatives
与其他工具对比
| Tool | Best For | Limitations |
|---|---|---|
| Polars | 1-100GB, speed critical | Must fit in RAM |
| Pandas | Small data, ecosystem | Slow, memory hungry |
| Dask | Larger than RAM | More complex API |
| Spark | Cluster computing | Infrastructure overhead |
| DuckDB | SQL interface | Different API style |
| 工具 | 最佳适用场景 | 局限性 |
|---|---|---|
| Polars | 1-100GB数据集、对速度要求高 | 数据必须可放入内存 |
| Pandas | 小型数据、生态完善 | 速度慢、内存占用高 |
| Dask | 超出内存的数据集 | API复杂度更高 |
| Spark | 集群计算 | 基础设施开销大 |
| DuckDB | SQL接口需求 | API风格差异大 |
Resources
参考资源
- Docs: https://pola.rs/
- User Guide: https://docs.pola.rs/user-guide/
- Cookbook: https://docs.pola.rs/user-guide/misc/cookbook/