跳转到主要内容

速度极快的DataFrame库

项目描述

Polars logo

文档: 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。

可以通过以下步骤按顺序完成此操作

  1. 安装最新的 Rust 编译器

  2. 安装 maturin: pip install maturin

  3. 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 polarsimport polars

在Python中使用自定义Rust函数

通过在Rust中编译的UDFs扩展Polars很容易。我们公开了针对 DataFrameSeries 数据结构的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 目标功能。

赞助商

JetBrains logo

项目详情


发行历史 发布通知 | RSS源

下载文件

下载适合您平台的应用程序。如果您不确定选择哪一个,请了解更多关于安装包的信息

源分发

polars-1.9.0.tar.gz (4.0 MB 查看哈希值)

上传时间

构建分发

polars-1.9.0-cp38-abi3-win_amd64.whl (32.8 MB 查看哈希值)

上传时间 CPython 3.8+ Windows x86-64

polars-1.9.0-cp38-abi3-manylinux_2_24_aarch64.whl (29.8 MB 查看哈希值)

上传于 CPython 3.8+ manylinux: glibc 2.24+ ARM64

polars-1.9.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (33.1 MB 查看哈希值)

上传于 CPython 3.8+ manylinux: glibc 2.17+ x86-64

polars-1.9.0-cp38-abi3-macosx_11_0_arm64.whl (28.2 MB 查看哈希值)

上传于 CPython 3.8+ macOS 11.0+ ARM64

polars-1.9.0-cp38-abi3-macosx_10_12_x86_64.whl (31.9 MB 查看哈希值)

上传于 CPython 3.8+ macOS 10.12+ x86-64

支持者:

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面