安装
Polars 是一个库,安装就像调用相应编程语言的包管理器一样简单。
pip install polars
# Or for legacy CPUs without AVX2 support
pip install polars-lts-cpu
cargo add polars -F lazy
# Or Cargo.toml
[dependencies]
polars = { version = "x", features = ["lazy", ...]}
大索引
默认情况下,Polars 数据框的行数限制为 \(2^{32}\) 行(约43亿)。通过启用大索引扩展,可以将此限制增加到 \(2^{64}\) 行(约180亿亿):
pip install polars-u64-idx
cargo add polars -F bigidx
# Or Cargo.toml
[dependencies]
polars = { version = "x", features = ["bigidx", ...] }
旧版CPU
要在没有AVX支持的旧CPU上安装Polars for Python,请运行:
pip install polars-lts-cpu
导入
要使用该库,只需将其导入到您的项目中:
import polars as pl
use polars::prelude::*;
功能标志
通过使用上述命令,您可以在系统上安装Polars的核心部分。然而,根据您的使用情况,您可能还希望安装可选的依赖项。这些依赖项被设为可选是为了最小化安装体积。根据编程语言的不同,标志也会有所不同。在用户指南中,我们会提到当使用的功能需要额外依赖时的情况。
Python
# For example
pip install 'polars[numpy,fsspec]'
全部
| Tag | Description |
|---|---|
| all | Install all optional dependencies. |
GPU
| Tag | Description |
|---|---|
| gpu | Run queries on NVIDIA GPUs. |
注意
请参阅GPU支持以获取更详细的说明和先决条件。
互操作性
| Tag | Description |
|---|---|
| pandas | Convert data to and from pandas dataframes/series. |
| numpy | Convert data to and from NumPy arrays. |
| pyarrow | Convert data to and from PyArrow tables/arrays. |
| pydantic | Convert data from Pydantic models to Polars. |
Excel
| Tag | Description |
|---|---|
| calamine | Read from Excel files with the calamine engine. |
| openpyxl | Read from Excel files with the openpyxl engine. |
| xlsx2csv | Read from Excel files with the xlsx2csv engine. |
| xlsxwriter | Write to Excel files with the XlsxWriter engine. |
| excel | Install all supported Excel engines. |
数据库
| Tag | Description |
|---|---|
| adbc | Read from and write to databases with the Arrow Database Connectivity (ADBC) engine. |
| connectorx | Read from databases with the ConnectorX engine. |
| sqlalchemy | Write to databases with the SQLAlchemy engine. |
| database | Install all supported database engines. |
云
| Tag | Description |
|---|---|
| fsspec | Read from and write to remote file systems. |
其他输入/输出
| Tag | Description |
|---|---|
| deltalake | Read from and write to Delta tables. |
| iceberg | Read from Apache Iceberg tables. |
其他
| Tag | Description |
|---|---|
| async | Collect LazyFrames asynchronously. |
| cloudpickle | Serialize user-defined functions. |
| graph | Visualize LazyFrames as a graph. |
| plot | Plot dataframes through the plot namespace. |
| style | Style dataframes through the style namespace. |
| timezone | Timezone support1. |
Rust
# Cargo.toml
[dependencies]
polars = { version = "0.26.1", features = ["lazy", "temporal", "describe", "json", "parquet", "dtype-datetime"] }
可选功能包括:
- 额外的数据类型:
dtype-datedtype-datetimedtype-timedtype-durationdtype-i8dtype-i16dtype-u8dtype-u16dtype-categoricaldtype-struct
lazy- 懒加载API:regex- 在列选择中使用正则表达式。dot_diagram- 从懒加载的逻辑计划创建点图。
sql- 将SQL查询传递给Polars。streaming- 能够处理大于内存的数据集。random- 生成包含随机采样值的数组ndarray- 从DataFrame转换为ndarraytemporal- 在Chrono和Polars之间进行时间数据类型的转换timezones- 激活时区支持。strings- 为StringChunked提供的额外字符串工具:string_pad- 用于pad_start,pad_end,zfill。string_to_integer- 用于parse_int。
object- 支持通用的ChunkedArrays,称为ObjectChunked(对T通用)。 这些可以通过Any特性从Series向下转换。- 性能相关:
nightly- 一些仅在夜间版本中可用的功能,如SIMD和特殊化。performant- 更多的快速路径,编译时间更长。bigidx- 如果您预计行数将超过 \(2^{32}\),请激活此功能。 这允许Polars通过使用u64作为索引来扩展到远远超过这个数字。 激活此功能后,Polars会稍微变慢,因为许多数据结构的缓存效率较低。cse- 激活公共子计划消除优化。
- IO相关:
- 数据框操作:
dynamic_group_by- 基于时间窗口而不是预定义键进行分组。同时激活滚动窗口分组操作。sort_multiple- 允许在多个列上对数据框进行排序。rows- 从行创建数据框并从dataframes中提取行。同时激活pivot和transpose操作。join_asof- ASOF连接,以连接最近的键而不是精确的相等匹配。cross_join- 创建两个数据框的笛卡尔积。semi_anti_join- SEMI和ANTI连接。row_hash- 将数据框行哈希到UInt64Chunked的实用工具。diagonal_concat- 对角连接,从而组合不同的模式。dataframe_arithmetic- 数据框与其他数据框或系列之间的算术运算。partition_by- 按组分割成多个数据框。
- Series/表达式操作:
is_in- 检查Series中的成员资格。zip_with- 压缩两个Series/ChunkedArray。round_series- 对Series的底层浮点类型进行四舍五入。repeat_by- 根据另一个数组指定的次数重复数组中的元素。is_first_distinct- 检查元素是否为第一个唯一值。is_last_distinct- 检查元素是否为最后一个唯一值。checked_arithmetic- 在无效操作时返回None的检查算术。dot_product- 在Series和表达式上进行点积/内积。concat_str- 在线性时间内连接字符串数据。reinterpret- 将位重新解释为有符号/无符号的实用工具。take_opt_iter- 使用Iterator从Series中获取。- >
mode- 返回最常出现的值。cum_agg-cum_sum,cum_min, 和cum_max聚合。rolling_window- 滚动窗口函数,如rolling_mean。interpolate- 插值None值。extract_jsonpath- 在StringChunked上运行jsonpath查询。list- 列表工具:list_gather- 通过多个索引获取子列表。rank- 排名算法。moment- 峰度和偏度统计。ewma- 指数移动平均窗口。abs- 获取Series的绝对值。arange- 在Series上进行范围操作。product- 计算Series的乘积。diff-diff操作。pct_change- 计算变化百分比。unique_counts- 计算表达式中的唯一值数量。log- Series的对数。list_to_struct- 将List转换为Struct数据类型。list_count- 计算列表中的元素数量。list_eval- 在列表元素上应用表达式。cumulative_eval- 在累积增加的窗口上应用表达式。arg_where- 获取条件成立的索引。search_sorted- 查找应插入元素以保持顺序的索引。offset_by- 为考虑月份和闰年的日期添加偏移量。trigonometry- 三角函数。sign- 计算Series的逐元素符号。propagate_nans-NaN传播的最小/最大聚合。
- Dataframe 美化打印:
fmt- 激活 dataframe 格式化。
-
仅当您在Windows上时才需要。 ↩