闪电般快的DataFrame库
项目描述
文档: Python - Rust - Node.js - R | StackOverflow: Python - Rust - Node.js - R | 用户指南 | Discord
Polars: 在 Rust、Python、Node.js、R 和 SQL 中提供闪电般快的 DataFrame
Polars 是一个 DataFrame 接口,它基于 Rust 实现的 OLAP 查询引擎,使用 Apache Arrow Columnar Format 作为内存模型。
- 懒加载 | 主动执行
- 多线程
- 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 (~4.2 billion) 行?使用 bigidx
功能标志编译Polars,或者对于Python用户,安装 pip install polars-u64-idx
。
除非您遇到行边界,否则请勿使用此功能,因为默认构建的Polars速度更快且内存消耗更少。
遗留
您希望Polars在旧CPU(例如,2011年之前的CPU)上运行,还是在Apple Silicon上的Rosetta下的 x86-64
Python构建上运行?安装 pip install polars-lts-cpu
。此版本的Polars未编译带有 AVX 目标功能。
赞助商
项目详情
polars_lts_cpu-1.9.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 4848ea9f54cb59cce6b08a5f161a5c488684b55729f2ef076c65ca92759a985e |
|
MD5 | 08177efa8d527e6e0aa9754adf9f4e1d |
|
BLAKE2b-256 | 30b881ada35f862f5b37456b225f578128ec4314da5a4627b440acf01180fbf9 |
polars_lts_cpu-1.9.0-cp38-abi3-win_amd64.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 0c199d86e3d6b775264e3a596ebfbae4c7848b88bed4248822b7b498b2e96f2d |
|
MD5 | 0d11dd2a6b530e0af4a569c3b4f4abd7 |
|
BLAKE2b-256 | 53b1d7236cbcef17de67938be6068ff72153bffed7543b496a93b31b88e0c670 |
polars_lts_cpu-1.9.0-cp38-abi3-manylinux_2_24_aarch64.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | e8f8eb6d9c5b017eb4ffb6276255b77d4eb8508e30bc2898e2b55ebe8bc9715c |
|
MD5 | 78cb1ae9ac33bb493ec1dd08d1ec32ec |
|
BLAKE2b-256 | 27f2cad4bccf463695de44869d17419f891b356b3af7275dcc64be95264317e5 |
polars_lts_cpu-1.9.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 9eb332c2e5837759a5cb406566a1d0feddf145711176cf89f745dba010c739a9 |
|
MD5 | 74d7a830c7fcb4d23e5c33a28cee99f3 |
|
BLAKE2b-256 | 69dfc393f41ad2da9cb0397436e3b424a1982b093d02181fc09ffbcefb98ff30 |
polars_lts_cpu-1.9.0-cp38-abi3-macosx_11_0_arm64.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | fb10fd9f8a8c0145ec70c4f3dde3ff1ef9f18079964c9dc40e8aac3d7521a862 |
|
MD5 | acabcd1eedb3a31b230ae66419724a4a |
|
BLAKE2b-256 | 0d362a816b333b2eeff12187026958cd2fac4a31f7f00e8a50f85ab08ed0531e |
哈希值 for polars_lts_cpu-1.9.0-cp38-abi3-macosx_10_12_x86_64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 1dd1484b9a08171184f198c7b1a1bdcea7457de4a7d481949ec410ee96a49bc9 |
|
MD5 | 319c3ba45f7495f8feb8f2b5d3bab835 |
|
BLAKE2b-256 | 9a7224127dc03627b0c50bef745b1ee0451359de17ba8e9a70027fdaf9f9c76e |