跳转到主要内容

闪电般的快速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中编译的UDF扩展Polars非常简单。我们公开了PyO3扩展,用于DataFrame和Series数据结构。更多信息请参阅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_u64_idx-1.9.0.tar.gz (4.0 MB 查看散列值)

上传时间

构建分发

polars_u64_idx-1.9.0-cp38-abi3-win_amd64.whl (32.8 MB 查看散列值)

上传时间 CPython 3.8+ Windows x86-64

polars_u64_idx-1.9.0-cp38-abi3-manylinux_2_24_aarch64.whl (29.8 MB 查看散列值)

上传时间 CPython 3.8+ manylinux: glibc 2.24+ ARM64

polars_u64_idx-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_u64_idx-1.9.0-cp38-abi3-macosx_11_0_arm64.whl (28.2 MB 查看散列值)

上传时间 CPython 3.8+ macOS 11.0+ ARM64

polars_u64_idx-1.9.0-cp38-abi3-macosx_10_12_x86_64.whl (31.9 MB 查看散列值)

上传时间 CPython 3.8+ macOS 10.12+ x86-64

支持者