You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust Polars无法访问str命名空间,遗漏了什么配置?

Polars中无法调用str()方法的问题解决

操作描述

将本地CSV数据加载到DataFrame后,已成功选择FILE列并打印,代码及输出如下:

use polars::prelude::*;
use polars::prelude::DataFrame;

<...>

let df_sim = match df_sim {
    Ok(df) => df,
    Err(e) => panic!("{}", e)
};

let df_new = df_sim
    .clone()
    .lazy()
    .select([
        col("FILE")
    ]).collect();

println!("{:?}", df_new); 

输出:

┌───────────────┐
│ FILE          │
│ ---           │
│ str           │
╞═══════════════╡
│ 0.jpg         │
│ hello.jpg     │
│ harry.jpg     │
└───────────────┘

问题

尝试在FILE列中查找包含harry的文件时,无法访问str命名空间,编译器找不到str()方法。相关代码:

let df_new = df_sim
    .clone()
    .lazy()
    .select([
        col("FILE")
            .str()
            .contains(lit("harry"), false)
            .alias("regex"),
    ]).collect();

println!("{:?}", df_new); 

编译错误:

error[E0599]: no method named `str` found for enum `Expr` in the current scope
   |
61 | /         col("FILE")
62 | |         .str()
   | |         -^^^ help: there is a method with a similar name: `std`
   | |_________|
   |

当前Cargo.toml中Polars依赖配置:

polars = {version ="0.38.2", features = ["lazy"]}

解决方法

Polars的字符串操作方法(如str())需要启用strings特性,修改Cargo.toml中的依赖配置,添加"strings"即可:

polars = {version ="0.38.2", features = ["lazy", "strings"]}

重新编译后,就能正常调用str()命名空间下的字符串处理方法了。

内容的提问来源于stack exchange,提问作者black_hole_sun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 11:02:39