pola-rs / pyo3-polars

Plugins/extension for Polars
MIT License
238 stars 39 forks source link

trait bound `polars::prelude::DataFrame: From<PyDataFrame>` is not satisfied #2

Closed wlwatkins closed 1 year ago

wlwatkins commented 1 year ago

Trying to select a column based on regex as in the example of polars.

but I seem to not be able to convert the PyDataFrame to a Dataframe, or lazyframe

Not sure if I'm doing it wrong, or if the library is not complet.

Error on pydf.into();

the trait bound `polars::prelude::DataFrame: From<PyDataFrame>` is not satisfied
required for `PyDataFrame` to implement `Into<polars::prelude::DataFrame>`
// Might have some unused imports
use ndarray::{Array2, Zip, Array1};
use numpy::{IntoPyArray, PyArray2, PyReadonlyArray2};
use polars::prelude::*;
use pyo3::{PyResult, Python, pyfunction};
use pyo3_polars::PyDataFrame;

#[pyfunction]
pub fn apply<'py>(_py: Python<'py>, pydf: PyDataFrame) -> PyResult<()> {

    let df: DataFrame = pydf.into();

    let df2 = df.clone().lazy().select([col("^col_*")]).collect();
    println!("{:?}", df2);
    Ok(())
}

I tried without the clone and lazy, but it still the same issue

    let df: DataFrame = pydf.into();
                         ^^^^ ---- required by a bound introduced by this call
                         |
                         the trait `From<PyDataFrame>` is not implemented for `polars::prelude::DataFrame`
    let df2 = df.select([col(&"^col_*")]).unwrap();
                 ^^^^^^ the trait `AsRef<str>` is not implemented for `Expr`

maybe it's simply a regex issue....

ritchie46 commented 1 year ago

Can you run the example in this repo?

LaurentErreca commented 1 year ago

Hi! I have almost the same issue when trying to run the example:

> maturin develop -m extend_polars/Cargo.toml

` ... Compiling polars-ops v0.27.2

Compiling polars-time v0.27.2

Compiling polars-io v0.27.2

Compiling polars-plan v0.27.2

Compiling polars-pipe v0.27.2

Compiling polars-lazy v0.27.2

Compiling extend_polars v0.1.0 (/my_local_path_to_polars/extend_polars)

error[E0277]: the trait bound polars_core::frame::DataFrame: From<PyDataFrame> is not satisfied --> src/lib.rs:13:25 13 let df: DataFrame = pydf.into(); ^^^^ ---- required by a bound introduced by this call
the trait From<PyDataFrame> is not implemented for polars_core::frame::DataFrame

= help: the trait From<&polars_core::prelude::Schema> is implemented for polars_core::frame::DataFrame = note: required for PyDataFrame to implement Into<polars_core::frame::DataFrame>

error[E0277]: the trait bound PyPolarsErr: From<PolarsError> is not satisfied --> src/lib.rs:14:14 14 let df = parallel_jaccard_mod::parallel_jaccard(df, col_a, col_b).map_err(PyPolarsErr::from)?; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait From<PolarsError> is not implemented for PyPolarsErr

= help: the following other types implement trait From<T>: <PyPolarsErr as From> <PyPolarsErr as From>

error[E0277]: the trait bound PyPolarsErr: From<PolarsError> is not satisfied --> src/lib.rs:14:79 14 let df = parallel_jaccard_mod::parallel_jaccard(df, col_a, col_b).map_err(PyPolarsErr::from)?; ^^^^^^^^^^^^^^^^^ the trait From<PolarsError> is not implemented for PyPolarsErr

= help: the following other types implement trait From<T>: <PyPolarsErr as From> <PyPolarsErr as From>

error[E0308]: mismatched types
--> src/lib.rs:15:20 15 Ok(PyDataFrame(df)) ----------- ^^ expected struct polars_core::frame::DataFrame, found a different struct polars_core::frame::DataFrame
arguments to this struct are incorrect
= note: struct `polars_core::frame::DataFrame` and struct `polars_core::frame::DataFrame` have similar names, but are actually distinct types

note: struct polars_core::frame::DataFrame is defined in crate polars_core `

I'm starting with rust and polars, so the problem may be between my screen and my chair :) Any help would be appreciated! Laurent

ritchie46 commented 1 year ago

This is fixed. You must ensure you use the same rust polars version as this crate does.

LaurentErreca commented 1 year ago

It works for me now, thanks @ritchie46 ! 👍

wlwatkins commented 1 year ago

Unfortunately, I moved to ndarray after all. However I think there's an underlying issue with rust since some libraries seem to crash if they import different versions of the same library.

For instance I tried nsharr to convert between ndarray and nalgebra. But it used an older version of the latter and thus couldn't compile.

It would be nice that rust either warns or even gives solutions to working with various libraries like this. It might be a solved problem (thinking of pip) though.