polars.LazyFrame.collect#

LazyFrame.collect(
*,
type_coercion: bool = True,
_type_check: bool = True,
predicate_pushdown: bool = True,
projection_pushdown: bool = True,
simplify_expression: bool = True,
slice_pushdown: bool = True,
comm_subplan_elim: bool = True,
comm_subexpr_elim: bool = True,
cluster_with_columns: bool = True,
collapse_joins: bool = True,
no_optimization: bool = False,
streaming: bool = False,
engine: EngineType = 'cpu',
background: bool = False,
_check_order: bool = True,
_eager: bool = False,
**_kwargs: Any,
) DataFrame | InProcessQuery[source]#

将此LazyFrame具体化为DataFrame。

默认情况下,所有查询优化都已启用。可以通过将相应的参数设置为False来禁用个别优化。

Parameters:
type_coercion

进行类型强制优化。

predicate_pushdown

执行谓词下推优化。

projection_pushdown

执行投影下推优化。

simplify_expression

运行简化表达式优化。

slice_pushdown

切片下推优化。

comm_subplan_elim

将尝试缓存出现在自连接或联合上的分支子计划。

comm_subexpr_elim

常见的子表达式将被缓存并重复使用。

cluster_with_columns

将连续的独立调用合并到with_columns

collapse_joins

将连接和过滤器合并为更快的连接

no_optimization

关闭(某些)优化。

streaming

分批处理查询以处理大于内存的数据。 如果设置为False(默认值),则整个查询将在单个批次中处理。

警告

流模式被认为是不稳定的。它可能会在任何时候更改,而不被视为破坏性更改。

注意

使用 explain() 来查看 Polars 是否可以在流模式下处理查询。

engine

选择用于处理查询的引擎,可选。 如果设置为"cpu"(默认),则使用 polars CPU引擎运行查询。如果设置为"gpu",则使用GPU引擎。 可以通过提供GPUEngine对象 来对GPU引擎进行细粒度控制,例如在具有多个设备的系统上使用哪个设备。

注意

GPU模式被认为是不稳定的。并非所有查询都能在GPU上成功运行,但是,如果不支持执行,它们应该会透明地回退到默认引擎。

使用POLARS_VERBOSE=1运行将提供查询回退的信息(以及原因)。

注意

GPU引擎不支持流式处理或在后台运行。如果启用了其中任何一个功能,则GPU执行将被关闭。

background

在后台运行查询并获取查询的句柄。 此句柄可用于获取结果或取消查询。

警告

后台模式被认为是不稳定的。它可能会在任何时候被更改,而不被视为破坏性更改。

Returns:
DataFrame

另请参阅

explain

打印使用collect评估的查询计划。

profile

收集LazyFrame并计算图中每个节点的时间。

polars.collect_all

同时收集多个LazyFrames。

polars.Config.set_streaming_chunk_size

设置流批处理的大小。

示例

>>> lf = pl.LazyFrame(
...     {
...         "a": ["a", "b", "a", "b", "b", "c"],
...         "b": [1, 2, 3, 4, 5, 6],
...         "c": [6, 5, 4, 3, 2, 1],
...     }
... )
>>> lf.group_by("a").agg(pl.all().sum()).collect()  
shape: (3, 3)
┌─────┬─────┬─────┐
│ a   ┆ b   ┆ c   │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ a   ┆ 4   ┆ 10  │
│ b   ┆ 11  ┆ 10  │
│ c   ┆ 6   ┆ 1   │
└─────┴─────┴─────┘

以流模式收集

>>> lf.group_by("a").agg(pl.all().sum()).collect(
...     streaming=True
... )  
shape: (3, 3)
┌─────┬─────┬─────┐
│ a   ┆ b   ┆ c   │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ a   ┆ 4   ┆ 10  │
│ b   ┆ 11  ┆ 10  │
│ c   ┆ 6   ┆ 1   │
└─────┴─────┴─────┘

在GPU模式下收集

>>> lf.group_by("a").agg(pl.all().sum()).collect(engine="gpu")  
shape: (3, 3)
┌─────┬─────┬─────┐
│ a   ┆ b   ┆ c   │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ b   ┆ 11  ┆ 10  │
│ a   ┆ 4   ┆ 10  │
│ c   ┆ 6   ┆ 1   │
└─────┴─────┴─────┘

控制使用的设备

>>> lf.group_by("a").agg(pl.all().sum()).collect(
...     engine=pl.GPUEngine(device=1)
... )  
shape: (3, 3)
┌─────┬─────┬─────┐
│ a   ┆ b   ┆ c   │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ b   ┆ 11  ┆ 10  │
│ a   ┆ 4   ┆ 10  │
│ c   ┆ 6   ┆ 1   │
└─────┴─────┴─────┘