gpt4 book ai didi

rust - 在 polars rust 中使用 groupby 时如何避免深度复制?

转载 作者:行者123 更新时间:2023-12-05 02:29:12 26 4
gpt4 key购买 nike

我有一个数据集,我需要对不同的列进行 groupby 操作。这是使用 polars 版本“0.21.1”的最小工作代码

use polars::prelude::*;
use polars_lazy::prelude::*;
use polars::df;

fn main(){
let df = df![
"x1" => ["a", "b", "c", "a"],
"x2" => ["A", "A", "B", "B"],
"y" => [1, 2, 3, 4],
].unwrap();

let lf: LazyFrame = df.lazy();

let out1 = groupby_x1(&lf);
println!("{:?}", out1.collect());
let out2 = groupby_x2(&lf);
println!("{:?}", out2.collect());

}

fn groupby_x1(lf: &LazyFrame) -> LazyFrame {
let lf1: LazyFrame = lf.clone().groupby([col("x1")]).agg([
col("y").sum().alias("y_sum"),
]);
lf1
}

fn groupby_x2(lf: &LazyFrame) -> LazyFrame {
let lf1: LazyFrame = lf.clone().groupby([col("x2")]).agg([
col("y").sum().alias("y_sum"),
]);
lf1
}

但在代码中,我正在对整个 lazyframe lf 进行深度复制(使用 lf.clone()。我怎样才能避免这种情况?如果我替换 lf.clone() 在函数 groupby_x1groupby_x2 中使用 lf 我得到以下错误

error[E0507]: cannot move out of `*lf` which is behind a shared reference
--> src/main.rs:22:24
|
22 | let lf1: LazyFrame = lf.groupby([col("x1")]).agg([
| ^^^^^^^^^^^^^^^^^^^^^^^ move occurs because `*lf` has type `polars_lazy::frame::LazyFrame`, which does not implement the `Copy` trait

error[E0507]: cannot move out of `*lf` which is behind a shared reference
--> src/main.rs:29:24
|
29 | let lf1: LazyFrame = lf.groupby([col("x2")]).agg([
| ^^^^^^^^^^^^^^^^^^^^^^^ move occurs because `*lf` has type `polars_lazy::frame::LazyFrame`, which does not implement the `Copy` trait

For more information about this error, try `rustc --explain E0507`.
error: could not compile `polars_try` due to 2 previous errors

最佳答案

极地 SeriesArc<Vec<ArrowRef>>附近的新类型.当你克隆一个 DataFrame只有 Arc 的引用计数递增。

换句话说,polars 从不进行深度克隆。 DataFrame 的克隆 super 便宜。

关于rust - 在 polars rust 中使用 groupby 时如何避免深度复制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72320911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com