Search code examples
rustpython-polarsrust-polars

Converting from Python-Polars to Rust-Polars


I have the following working Python polars code. I am learning Rust and am interested in converting Python to Rust.

df = df.with_columns(pl.concat([pl.col(base).slice(0, period).rolling_mean(period), pl.col(base).slice(period,None)]).alias('con'))

How to convert the same in Rust? It might be very trivial, still not sure where I am going wrong.

let rolling_options = RollingOptions {
        window_size : Duration::parse(duration_str.as_str()),
        ..Default::default()
    };
let a = col(base).slice(0,period).rolling_mean(rolling_options);
let b = col(base).slice(period,lit(Null {}));

let temp_df = df.with_column(concat([a, b], UnionArgs::default()));

I keep getting the following error

mismatched types expected enum Expr found enum Result<LazyFrame, PolarsError>

When i checked the data types of a and b, to my surprise they are LazyFrame and not Expr

rolling_mean as per the document returns Expr and so does slice. Not sure what I am missing.


Solution

  • Working code.

    let duration_str = format!("{}{}",usize::try_from(period).unwrap().to_string().as_str(),"i");
    
    let rolling_options = RollingOptions {
        window_size : Duration::parse(duration_str.as_str()),
        min_periods : usize::try_from(period).unwrap(),
        ..Default::default()
    };
    
    let lf = df.with_column(concat_expr([col(base).slice(0,period).rolling_mean(rolling_options), 
        col(base).slice(period,lit(Null {}))], false)?.alias("con"));