最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Using `is_in` in rust-polars - Stack Overflow

programmeradmin0浏览0评论

I am trying to subset a rust-polars dataframe by the names contained in a different frame:

use polars::prelude::*;

fn main() {
    let mut df = df! [
        "names" => ["a", "b", "c", "d"],
        "values" => [1, 2, 3, 4],
        "floats" => [1.25, 2., 1., 0.5]
    ].unwrap();
//    println!("{:?}", df);

    let mut df1 = df! [
        "names" => ["b", "c"],
        "values" => [2, 3]
    ].unwrap();
//    println!("original df: {:?}", df1);

    let df2 = df
        .lazy()
        .filter(col("names").is_in(df1.column("names")))
        .collect()
        .unwrap();
//    println!("{:?}", df2);
}

This results in an error message:

error[E0599]: no method named `is_in` found for enum `Expr` in the current scope
   --> src/main.rs:22:30
    |
22  |         .filter(col("names").is_in(df1.column("names")))
    |                              ^^^^^
    |
help: there is a method `is_finite` with a similar name, but with different arguments
   --> /home/puller/.cargo/registry/src/index.crates.io-6f17d22bba15001f/polars-plan-0.46.0/src/dsl/mod.rs:781:5
    |
781 |     pub fn is_finite(self) -> Self {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0599`.
error: could not compile `drop_row` (bin "drop_row") due to 1 previous error

This page suggests that is_in is "Available on crate feature polars-ops only." However adding "polars-ops" to my Cargo.toml didn't help:

[dependencies]
polars = {version = "0.46.0", features = ["lazy", "polars-ops"]}

Moreover, the signature of is_in seems to differ from the one discussed in this question. So it is unclear, if the feature has been depreciated or available from another crate or something else.

I also tried

    let df2 = df
        .lazy()
        .filter(is_in(col("names"), df1.column("names")))
        .collect()
        .unwrap();
    println!("{:?}", df2);

in which case the error message is

error[E0425]: cannot find function `is_in` in this scope
  --> src/main.rs:30:17
   |
30 |         .filter(is_in(col("names"), df1.column("names")))
   |                 ^^^^^ not found in this scope

For more information about this error, try `rustc --explain E0425`.
error: could not compile `drop_row` (bin "drop_row") due to 1 previous error

Update
Looking more carefully at the threat linked earlier, the correct signature is this one, from crate is_in. The error message is no

error[E0277]: the trait bound `Expr: From<&polars::prelude::Column>` is not satisfied
    --> src/main.rs:19:36
     |
19   |         .filter(col("names").is_in(df1.column("names").unwrap()))
     |                              ----- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<&polars::prelude::Column>` is not implemented for `Expr`
     |                              |
     |                              required by a bound introduced by this call
     |
     = help: the following other types implement trait `From<T>`:
               `Expr` implements `From<&str>`
               `Expr` implements `From<AggExpr>`
               `Expr` implements `From<bool>`
               `Expr` implements `From<f32>`
               `Expr` implements `From<f64>`
               `Expr` implements `From<i16>`
               `Expr` implements `From<i32>`
               `Expr` implements `From<i64>`
             and 3 others
     = note: required for `&polars::prelude::Column` to implement `Into<Expr>`
note: required by a bound in `polars_plan::dsl::<impl Expr>::is_in`
    --> /home/puller/.cargo/registry/src/index.crates.io-6f17d22bba15001f/polars-plan-0.46.0/src/dsl/mod.rs:1190:21
     |
1190 |     pub fn is_in<E: Into<Expr>>(self, other: E) -> Self {
     |                     ^^^^^^^^^^ required by this bound in `polars_plan::dsl::<impl Expr>::is_in`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `drop_row` (bin "drop_row") due to 1 previous error

I am trying to subset a rust-polars dataframe by the names contained in a different frame:

use polars::prelude::*;

fn main() {
    let mut df = df! [
        "names" => ["a", "b", "c", "d"],
        "values" => [1, 2, 3, 4],
        "floats" => [1.25, 2., 1., 0.5]
    ].unwrap();
//    println!("{:?}", df);

    let mut df1 = df! [
        "names" => ["b", "c"],
        "values" => [2, 3]
    ].unwrap();
//    println!("original df: {:?}", df1);

    let df2 = df
        .lazy()
        .filter(col("names").is_in(df1.column("names")))
        .collect()
        .unwrap();
//    println!("{:?}", df2);
}

This results in an error message:

error[E0599]: no method named `is_in` found for enum `Expr` in the current scope
   --> src/main.rs:22:30
    |
22  |         .filter(col("names").is_in(df1.column("names")))
    |                              ^^^^^
    |
help: there is a method `is_finite` with a similar name, but with different arguments
   --> /home/puller/.cargo/registry/src/index.crates.io-6f17d22bba15001f/polars-plan-0.46.0/src/dsl/mod.rs:781:5
    |
781 |     pub fn is_finite(self) -> Self {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0599`.
error: could not compile `drop_row` (bin "drop_row") due to 1 previous error

This page suggests that is_in is "Available on crate feature polars-ops only." However adding "polars-ops" to my Cargo.toml didn't help:

[dependencies]
polars = {version = "0.46.0", features = ["lazy", "polars-ops"]}

Moreover, the signature of is_in seems to differ from the one discussed in this question. So it is unclear, if the feature has been depreciated or available from another crate or something else.

I also tried

    let df2 = df
        .lazy()
        .filter(is_in(col("names"), df1.column("names")))
        .collect()
        .unwrap();
    println!("{:?}", df2);

in which case the error message is

error[E0425]: cannot find function `is_in` in this scope
  --> src/main.rs:30:17
   |
30 |         .filter(is_in(col("names"), df1.column("names")))
   |                 ^^^^^ not found in this scope

For more information about this error, try `rustc --explain E0425`.
error: could not compile `drop_row` (bin "drop_row") due to 1 previous error

Update
Looking more carefully at the threat linked earlier, the correct signature is this one, from crate is_in. The error message is no

error[E0277]: the trait bound `Expr: From<&polars::prelude::Column>` is not satisfied
    --> src/main.rs:19:36
     |
19   |         .filter(col("names").is_in(df1.column("names").unwrap()))
     |                              ----- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<&polars::prelude::Column>` is not implemented for `Expr`
     |                              |
     |                              required by a bound introduced by this call
     |
     = help: the following other types implement trait `From<T>`:
               `Expr` implements `From<&str>`
               `Expr` implements `From<AggExpr>`
               `Expr` implements `From<bool>`
               `Expr` implements `From<f32>`
               `Expr` implements `From<f64>`
               `Expr` implements `From<i16>`
               `Expr` implements `From<i32>`
               `Expr` implements `From<i64>`
             and 3 others
     = note: required for `&polars::prelude::Column` to implement `Into<Expr>`
note: required by a bound in `polars_plan::dsl::<impl Expr>::is_in`
    --> /home/puller/.cargo/registry/src/index.crates.io-6f17d22bba15001f/polars-plan-0.46.0/src/dsl/mod.rs:1190:21
     |
1190 |     pub fn is_in<E: Into<Expr>>(self, other: E) -> Self {
     |                     ^^^^^^^^^^ required by this bound in `polars_plan::dsl::<impl Expr>::is_in`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `drop_row` (bin "drop_row") due to 1 previous error
Share Improve this question edited yesterday Roger V. asked yesterday Roger V.Roger V. 7715 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Here is how you could do this. In order to compile this, You must enable is_in feature in Cargo.toml file. The lit is used here to create expression from Series. This is because is_in requires it's argument to be an expression type.

use polars::prelude::*;

fn main() {
    let df = df! [
        "names" => ["a", "b", "c", "d"],
        "values" => [1, 2, 3, 4],
        "floats" => [1.25, 2., 1., 0.5]
    ]
    .unwrap();
    println!("{:?}", df);

    let df1 = df! [
        "names" => ["b", "c"],
        "values" => [2, 3]
    ]
    .unwrap();
    println!("original df: {:?}", df1);

    let names_as_series = df1
        .column("names")
        .unwrap()
        .as_materialized_series()
        .clone();

    let df2 = df
        .lazy()
        .filter(col("names").is_in(lit(names_as_series)))
        .collect()
        .unwrap();
    
    println!("{:?}", df2);
}
发布评论

评论列表(0)

  1. 暂无评论