在 Rust/Polars 中对 ChunkedArray 类型进行抽象

问题描述 投票:0回答:1

我正在尝试编写一个特征,让我可以抽象地将函数应用于极坐标列中的每个元素/单元格,并将结果添加到现有(惰性)数据帧中。我目前坚持将通用

ChunkedArray
转换为
Series
,并且基于
IntoSeries
ChunkedArray
的特征绑定(参见 here),我不太明白如何做到这一点。具体来说,目前我有:

trait MapCell: Send + Sync + Sized + 'static {
    type CellType : PolarsDataType + 'static ;
    type NativeType;
    type Builder: ChunkedBuilder<Self::NativeType, Self::CellType>;

    fn map(self, column:Expr, result_name:&str) -> anyhow::Result<Expr> {
        let output_type = GetOutput::from_type(self.datatype());

        Ok(column.map(move |col| {
            let mut builder = self.builder();
            for content in col.str()?.into_no_null_iter() {
                builder.append_value(self.apply(content));
            }
            let ca = builder.finish();
            Ok(Some(builder.finish().into_series()))
        }, output_type)
        .alias(result_name))
    }

    fn apply(&self, content: &str) -> Self::NativeType;

    fn builder(&self) -> Self::Builder;

    fn datatype(&self) -> DataType;
}

这给了我:

error[E0599]: the method `into_series` exists for struct `ChunkedArray<<Self as MapCell>::CellType>`, but its trait bounds were not satisfied
   --> src/lib.rs:37:38
    |
37  |             Ok(Some(builder.finish().into_series()))
    |                                      ^^^^^^^^^^^ method cannot be called on `ChunkedArray<<Self as MapCell>::CellType>` due to unsatisfied trait bounds
    |
   ::: /Users/johsulli/.cargo/registry/src/index.crates.io-6f17d22bba15001f/polars-core-0.37.0/src/chunked_array/mod.rs:136:1
    |
136 | pub struct ChunkedArray<T: PolarsDataType> {
    | ------------------------------------------ doesn't satisfy `_: IntoSeries`
    |
   ::: /Users/johsulli/.cargo/registry/src/index.crates.io-6f17d22bba15001f/polars-core-0.37.0/src/series/implementations/mod.rs:51:1
    |
51  | pub(crate) struct SeriesWrap<T>(pub T);
    | ------------------------------- doesn't satisfy `_: SeriesTrait`
    |
    = note: the following trait bounds were not satisfied:
            `polars::series::implementations::SeriesWrap<polars::prelude::ChunkedArray<<Self as MapCell>::CellType>>: polars::prelude::SeriesTrait`
            which is required by `polars::prelude::ChunkedArray<<Self as MapCell>::CellType>: polars::prelude::IntoSeries`

但我不知道如何满足这些界限,因为我无法访问

SeriesWrap
。在这种情况下,如何对
ChunkedArray
的类型进行抽象?

rust rust-polars
1个回答
0
投票

您可以要求 `ChunkedArray: IntoSeries:

trait MapCell: Send + Sync + Sized + 'static
where
    ChunkedArray<Self::CellType>: IntoSeries,
{
    type CellType: PolarsDataType + 'static;
    type NativeType;
    type Builder: ChunkedBuilder<Self::NativeType, Self::CellType>;

    fn map(self, column: Expr, result_name: &str) -> PolarsResult<Expr> {
        let output_type = GetOutput::from_type(self.datatype());

        Ok(column
            .map(
                move |col| {
                    let mut builder = self.builder();
                    for content in col.str()?.into_no_null_iter() {
                        builder.append_value(self.apply(content));
                    }
                    Ok(Some(builder.finish().into_series()))
                },
                output_type,
            )
            .alias(result_name))
    }

    fn apply(&self, content: &str) -> Self::NativeType;

    fn builder(&self) -> Self::Builder;

    fn datatype(&self) -> DataType;
}
© www.soinside.com 2019 - 2024. All rights reserved.