我有一个数据集,需要对不同的列执行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()
.我如何避免呢?如果在函数groupby_x1
和groupby_x2
中用lf
替换lf.clone()
,我会得到以下错误
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