速度极快的DataFrame库
项目描述
文档: Python - Rust - Node.js - R | StackOverflow: Python - Rust - Node.js - R | 用户指南 | Discord
Polars: Rust, Python, Node.js, R 和 SQL 中的闪电般快速 DataFrame
Polars 是一个 DataFrame 接口,它位于使用 Apache Arrow Columnar Format 作为内存模型的 Rust 实现的 OLAP 查询引擎之上。
- 懒加载 | 贪婪执行
- 多线程
- SIMD
- 查询优化
- 强大的表达式 API
- 混合流(大于 RAM 的数据集)
- Rust | Python | NodeJS | R | ...
要了解更多信息,请阅读 用户指南。
Python
>>> import polars as pl
>>> df = pl.DataFrame(
...     {
...         "A": [1, 2, 3, 4, 5],
...         "fruits": ["banana", "banana", "apple", "apple", "banana"],
...         "B": [5, 4, 3, 2, 1],
...         "cars": ["beetle", "audi", "beetle", "beetle", "beetle"],
...     }
... )
# embarrassingly parallel execution & very expressive query language
>>> df.sort("fruits").select(
...     "fruits",
...     "cars",
...     pl.lit("fruits").alias("literal_string_fruits"),
...     pl.col("B").filter(pl.col("cars") == "beetle").sum(),
...     pl.col("A").filter(pl.col("B") > 2).sum().over("cars").alias("sum_A_by_cars"),
...     pl.col("A").sum().over("fruits").alias("sum_A_by_fruits"),
...     pl.col("A").reverse().over("fruits").alias("rev_A_by_fruits"),
...     pl.col("A").sort_by("B").over("fruits").alias("sort_A_by_B_by_fruits"),
... )
shape: (5, 8)
┌──────────┬──────────┬──────────────┬─────┬─────────────┬─────────────┬─────────────┬─────────────┐
│ fruits   ┆ cars     ┆ literal_stri ┆ B   ┆ sum_A_by_ca ┆ sum_A_by_fr ┆ rev_A_by_fr ┆ sort_A_by_B │
│ ---      ┆ ---      ┆ ng_fruits    ┆ --- ┆ rs          ┆ uits        ┆ uits        ┆ _by_fruits  │
│ str      ┆ str      ┆ ---          ┆ i64 ┆ ---         ┆ ---         ┆ ---         ┆ ---         │
│          ┆          ┆ str          ┆     ┆ i64         ┆ i64         ┆ i64         ┆ i64         │
╞══════════╪══════════╪══════════════╪═════╪═════════════╪═════════════╪═════════════╪═════════════╡
│ "apple"  ┆ "beetle" ┆ "fruits"     ┆ 11  ┆ 4           ┆ 7           ┆ 4           ┆ 4           │
│ "apple"  ┆ "beetle" ┆ "fruits"     ┆ 11  ┆ 4           ┆ 7           ┆ 3           ┆ 3           │
│ "banana" ┆ "beetle" ┆ "fruits"     ┆ 11  ┆ 4           ┆ 8           ┆ 5           ┆ 5           │
│ "banana" ┆ "audi"   ┆ "fruits"     ┆ 11  ┆ 2           ┆ 8           ┆ 2           ┆ 2           │
│ "banana" ┆ "beetle" ┆ "fruits"     ┆ 11  ┆ 4           ┆ 8           ┆ 1           ┆ 1           │
└──────────┴──────────┴──────────────┴─────┴─────────────┴─────────────┴─────────────┴─────────────┘
SQL
>>> df = pl.scan_csv("docs/assets/data/iris.csv")
>>> ## OPTION 1
>>> # run SQL queries on frame-level
>>> df.sql("""
...	SELECT species,
...	  AVG(sepal_length) AS avg_sepal_length
...	FROM self
...	GROUP BY species
...	""").collect()
shape: (3, 2)
┌────────────┬──────────────────┐
│ species    ┆ avg_sepal_length │
│ ---        ┆ ---              │
│ str        ┆ f64              │
╞════════════╪══════════════════╡
│ Virginica  ┆ 6.588            │
│ Versicolor ┆ 5.936            │
│ Setosa     ┆ 5.006            │
└────────────┴──────────────────┘
>>> ## OPTION 2
>>> # use pl.sql() to operate on the global context
>>> df2 = pl.LazyFrame({
...    "species": ["Setosa", "Versicolor", "Virginica"],
...    "blooming_season": ["Spring", "Summer", "Fall"]
...})
>>> pl.sql("""
... SELECT df.species,
...     AVG(df.sepal_length) AS avg_sepal_length,
...     df2.blooming_season
... FROM df
... LEFT JOIN df2 ON df.species = df2.species
... GROUP BY df.species, df2.blooming_season
... """).collect()
您还可以使用 Polars CLI 直接在终端运行 SQL 命令。
# run an inline SQL query
> polars -c "SELECT species, AVG(sepal_length) AS avg_sepal_length, AVG(sepal_width) AS avg_sepal_width FROM read_csv('docs/assets/data/iris.csv') GROUP BY species;"
# run interactively
> polars
Polars CLI v0.3.0
Type .help for help.
> SELECT species, AVG(sepal_length) AS avg_sepal_length, AVG(sepal_width) AS avg_sepal_width FROM read_csv('docs/assets/data/iris.csv') GROUP BY species;
有关更多信息,请参阅 Polars CLI 存储库。
性能 🚀🚀
闪电般快速
Polars 非常快。事实上,它是性能最佳的解决方案之一。请参阅 PDS-H 基准测试结果。
轻量级
Polars 同样非常轻量。它不包含任何必需的依赖项,这在导入时间中体现出来。
- polars: 70ms
- numpy: 104ms
- pandas: 520ms
处理大于 RAM 的数据
如果您有不适合内存的数据,Polars 的查询引擎可以以流式方式处理您的查询(或查询的一部分)。这极大地降低了内存需求,因此您可能能够在笔记本电脑上处理 250GB 的数据集。使用 collect(streaming=True) 收集以流式方式运行查询。(这可能稍微慢一点,但仍然非常快!)
设置
Python
使用以下命令安装最新版本的 Polars:
pip install polars
我们还有一个 conda 包(conda install -c conda-forge polars),但是 pip 是安装 Polars 的首选方式。
使用所有可选依赖项安装 Polars。
pip install 'polars[all]'
您也可以安装所有可选依赖项的一个子集。
pip install 'polars[numpy,pandas,pyarrow]'
有关可选依赖项的更多详细信息,请参阅 用户指南。
要查看当前的 Polars 版本及其所有可选依赖项的完整列表,请运行
pl.show_versions()
目前版本发布非常频繁(每周/每隔几天),因此定期更新 Polars 以获取最新的错误修复/功能可能不是一件坏事。
Rust
您可以从 crates.io 获取最新版本,或者如果您想使用最新的功能/性能改进,请指向此存储库的 main 分支。
polars = { git = "https://github.com/pola-rs/polars", rev = "<optional git tag>" }
需要 Rust 版本 >=1.80。
贡献
想要贡献?请阅读我们的 贡献指南。
Python:从源码编译 Polars
如果您想获取边缘版本或最大性能,应从源码编译 Polars。
可以通过以下步骤按顺序完成此操作
- 
安装最新的 Rust 编译器 
- 
安装 maturin: pip install maturin
- 
在 cd py-polars中选择以下操作之一- make build-release,最快的二进制文件,编译时间非常长
- make build-opt,具有调试符号的快速二进制文件,编译时间较长
- make build-debug-opt,中等速度的二进制文件,具有调试断言和符号,编译时间中等
- make build,具有调试断言和符号的慢速二进制文件,编译时间快
 添加 -native(例如make build-release-native)以启用针对您CPU的特定优化。然而,这会产生不可移植的二进制/轮文件。
请注意,实现Python绑定的Rust包称为 py-polars 以区分包装的Rust包本身 polars。然而,Python包和Python模块都命名为 polars,因此您可以 pip install polars 和 import polars。
在Python中使用自定义Rust函数
通过在Rust中编译的UDFs扩展Polars很容易。我们公开了针对 DataFrame 和 Series 数据结构的PyO3扩展。更多内容请参阅 https://github.com/pola-rs/pyo3-polars。
更大...
您预计会有超过2^32(约42亿)行吗?使用带有 bigidx 功能标志编译Polars或对于Python用户,安装 pip install polars-u64-idx。
除非您遇到行边界,否则请勿使用此功能,因为Polars的默认构建更快且占用更少的内存。
旧版
您希望Polars在旧CPU(例如2011年之前的)上运行,还是在苹果硅上的 x86-64 Python构建下通过Rosetta运行?安装 pip install polars-lts-cpu。此版本的Polars未编译具有 AVX 目标功能。
赞助商
项目详情
下载文件
下载适合您平台的应用程序。如果您不确定选择哪一个,请了解更多关于安装包的信息。
源分发
构建分发
polars-1.9.0.tar.gz 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 8e1206ef876f61c1d50a81e102611ea92ee34631cb135b46ad314bfefd3cb122 | |
| MD5 | 76981175e64215117fa667904fdf5815 | |
| BLAKE2b-256 | 5309c2fb0b231d551e0c8e68097d08577712bdff1ba91346cda8228e769602f5 | 
polars-1.9.0-cp38-abi3-win_amd64.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 95de07066cd797dd940fa2783708a7bef93c827a57be0f4dfad3575a6144212b | |
| MD5 | 36c3f2950b281edd5404ed2ddf2b0786 | |
| BLAKE2b-256 | a6cff9170a3ac20e0efb9d3c1cdacc677e35b711ffd5ec48a6d5f3da7b7d8663 | 
polars-1.9.0-cp38-abi3-manylinux_2_24_aarch64.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | f753c8941a3b3249d59262d68a856714a96a7d4e16977aefbb196be0c192e151 | |
| MD5 | 47902161b0718d8688a0ab74c6593330 | |
| BLAKE2b-256 | e525bf5d43dcb538bf6573b15f3d5995a52be61b8fbce0cd737e72c4d25eef88 | 
polars-1.9.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | f85f132732aa63c6f3b502b0fdfc3ba9f0b78cc6330059b5a2d6f9fd78508acb | |
| MD5 | a36d5e642adb679f9be07625a7c9f219 | |
| BLAKE2b-256 | bb57b286b317f061d8f17bab4726a27e7b185fbf3d3db65cf689074256ea34a9 | 
polars-1.9.0-cp38-abi3-macosx_11_0_arm64.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 94b12d731cd200d2c50b13fc070d6353f708e632bca6529c5a72aa6a69e5285d | |
| MD5 | 2fbc643300bfe31b32833997e89852a7 | |
| BLAKE2b-256 | eebe15af97f4d8b775630da16a8bf0141507d9c0ae5f2637b9a27ed337b3b1ba | 
polars-1.9.0-cp38-abi3-macosx_10_12_x86_64.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | a471d2ce96f6fa5dd0ef16bcdb227f3dbe3af8acb776ca52f9e64ef40c7489a0 | |
| MD5 | 7cbf05deceef8e9cca49fc13e1b1af22 | |
| BLAKE2b-256 | 64cc3d0292048d8f9045a03510aeecda2e6ed9df451ae8853274946ff841f98b |