Rust Polars - 从 df.column 获取结构系列而不是“&”引用

p6s*_*eve 10 rust raku rust-polars

我正在构建一个从 Raku NativeCall 到 Rust Polars 的接口,以获得很酷的 Arrow2 性能提升。在高层,我想使用 Polars 结构(例如 DataFrame 和 Series)作为匹配容器的属性。所以df.column我想要这样的东西......

use polars::prelude::*;//{CsvReader, DataType, Field, Schema, DataFrame,};
use polars::prelude::{Result as PolarResult};
use polars::frame::DataFrame;
use polars::datatypes::DataType;

pub struct DataFrameC {
    df: DataFrame,
}

impl DataFrameC {
    fn new() -> DataFrameC {
        DataFrameC {
            df: DataFrame::default(),
        }   
    }   

    fn column(&self, string: String) -> Series {
        //let colin = self.df.column(&string).unwrap().head(Some(23));
        let colin = self.df.column(&string).unwrap()
        println!{"{}", colin};
        colin
    }
}
Run Code Online (Sandbox Code Playgroud)

(系列的类似方法 - 因此完成此 fn 的下一步是创建 aSeries::new()然后se.set(colin)

但是 - 我无法弄清楚如何取消引用 Polars 系列和对普通系列的引用(我已经尝试过 .Deref() 和 .from_ptr() 但这些方法不存在)。

我已经发现 Series.head() 确实返回一个 Series 结构 --- 所以 // 行按预期工作(但不是整个 Series!)

我不断收到此错误:

error[E0308]: mismatched types
  --> src/lib.rs:92:9
   |
88 |     fn column(&self, string: String) -> Series {
   |                                         ------ expected `polars::prelude::Series` because of return type
...
92 |         colin
   |         ^^^^^ expected struct `polars::prelude::Series`, found `&polars::prelude::Series`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `dan` due to previous error
Run Code Online (Sandbox Code Playgroud)

是否有执行此 deref 操作的习惯用法?

任何建议非常感谢!

rit*_*e46 13

你不能解引用 a &Seriesinto Series,因为Series不是Copy&Series您从中获得的内容self.df.column(&string).unwrap()归 拥有DataFrame

要返回 a Series,您必须克隆它。别担心,它是一个超级便宜的克隆,因为 aSeries只是一个Arc<ChunkedArray>,所以克隆只执行单个原子引用计数操作。

  • 是的,这可以解决问题 ```let colin = self.df.column(&amp;string).unwrap().clone();``` 谢谢! (3认同)