Rust Polars 四苦八苦メモ

use polars_core::prelude::*;
use polars_io::prelude::*;
use std::fs::File;

fn example() -> PolarsResult<DataFrame> {
    CsvReadOptions::default()
            .with_has_header(true)
            .try_into_reader_with_file_path(Some("data.csv".into()))?
            .finish()
}

fn main() -> PolarsResult<()> {
    let df = example()?;
    println!("{:?}", df);
    Ok(())
}

公式ドキュメントのものをまるっとコピーしたのに動かんのだけど・・・

cargo run
   Compiling try_polars v0.1.0 (/Users/shirotabi/Project/project/Rust/try_polars)
error[E0433]: failed to resolve: use of undeclared crate or module `polars_core`
 --> src/main.rs:1:5
  |
1 | use polars_core::prelude::*;
  |     ^^^^^^^^^^^ use of undeclared crate or module `polars_core`

error[E0433]: failed to resolve: use of undeclared crate or module `polars_io`
 --> src/main.rs:2:5
  |
2 | use polars_io::prelude::*;
  |     ^^^^^^^^^ use of undeclared crate or module `polars_io`
  |
help: there is a crate or module with a similar name
  |
2 | use polars::prelude::*;
  |     ~~~~~~

error[E0432]: unresolved imports `polars_core::prelude::*`, `polars_io::prelude::*`
 --> src/main.rs:1:5
  |
1 | use polars_core::prelude::*;
  |     ^^^^^^^^^^^^^^^^^^^^^^^
2 | use polars_io::prelude::*;
  |     ^^^^^^^^^^^^^^^^^^^^^

error[E0412]: cannot find type `PolarsResult` in this scope
 --> src/main.rs:5:17
  |
5 | fn example() -> PolarsResult<DataFrame> {
  |                 ^^^^^^^^^^^^ not found in this scope
  |
help: consider importing this type alias
  |
1 + use polars::error::PolarsResult;
  |

error[E0412]: cannot find type `DataFrame` in this scope
 --> src/main.rs:5:30
  |
5 | fn example() -> PolarsResult<DataFrame> {
  |                              ^^^^^^^^^ not found in this scope
  |
help: consider importing this struct
  |
1 + use polars::frame::DataFrame;
  |

error[E0433]: failed to resolve: use of undeclared type `CsvReadOptions`
 --> src/main.rs:6:5
  |
6 |     CsvReadOptions::default()
  |     ^^^^^^^^^^^^^^ use of undeclared type `CsvReadOptions`
  |
help: consider importing this struct
  |
1 + use polars::prelude::CsvReadOptions;
  |

error[E0412]: cannot find type `PolarsResult` in this scope
  --> src/main.rs:12:14
   |
12 | fn main() -> PolarsResult<()> {
   |              ^^^^^^^^^^^^ not found in this scope
   |
help: consider importing this type alias
   |
1  + use polars::error::PolarsResult;
   |

warning: unused import: `std::fs::File`
 --> src/main.rs:3:5
  |
3 | use std::fs::File;
  |     ^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

Some errors have detailed explanations: E0412, E0432, E0433.
For more information about an error, try `rustc --explain E0412`.
warning: `try_polars` (bin "try_polars") generated 1 warning
error: could not compile `try_polars` (bin "try_polars") due to 7 previous errors; 1 warning emitt

Cargo.toml

[package]
name = "try_polars"
version = "0.1.0"
edition = "2021"

[dependencies]
polars = { version = "0.45.0", features = ["csv"] }

わけわからんぞ・・・

use polars::prelude::*; // Polars全般の機能をインポート
use std::fs::File;

fn example() -> PolarsResult<DataFrame> {
    CsvReadOptions::default()
            .with_has_header(true)
            .try_into_reader_with_file_path(Some("data.csv".into()))?
            .finish()
}

fn main() -> PolarsResult<()> {
    let df = example()?;
    println!("{:?}", df);
    Ok(())
}
 cargo run
   Compiling try_polars v0.1.0 (/Users/shirotabi/Project/project/Rust/try_polars)
warning: unused import: `std::fs::File`
 --> src/main.rs:2:5
  |
2 | use std::fs::File;      // ファイル操作用のモジュールをインポート
  |     ^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: `try_polars` (bin "try_polars") generated 1 warning (run `cargo fix --bin "try_polars"` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.74s
     Running `target/debug/try_polars`
shape: (9_999, 1)
┌─────────────────────┐
│ 0.22445966924897276 │
│ ---                 │
│ f64                 │
╞═════════════════════╡
│ 0.655784            │
│ 0.93182             │
│ 0.589129            │
│ 0.194309            │
│ 0.316218            │
│ …                   │
│ 0.221614            │
│ 5.0                 │
│ 0.804628            │
│ 0.431909            │
│ 0.399103            │
└─────────────────────┘

お!動いた
けど、なんか警告出ているから、不要なimport消した。

cargi fix --bin "プロジェクト名"でなんか勝手に改善してくれるらしい
ただ、git使っていてコミットされていないものがあると警告が出る。

で、なんか

error: the working directory of this package has uncommitted changes, and `cargo fix` can potentially perform destructive changes; if you'd like to suppress this error pass `--allow-dirty`, `--allow-staged`, or commit the changes to these files:

っていうのが出た

pass --allow-dirty, --allow-staged, or commit the changes

  • 以下の3つの選択肢のいずれかを選んでください:

    • --allow-dirty: 未コミットの変更がある状態でも cargo fix を実行する。

    • --allow-staged: ステージングされた変更がある状態で cargo fix を実行する。

    • 変更をコミットする: すべての変更をGitにコミットした後に cargo fix を実行する。

とりあえず--allow-dirtyやって最終的に以下で成功

use polars::prelude::*; // Polars全般の機能をインポート

fn example() -> PolarsResult<DataFrame> {
    CsvReadOptions::default()
        .with_has_header(true)
        .try_into_reader_with_file_path(Some("data.csv".into()))?
        .finish()
}

fn main() -> PolarsResult<()> {
    let df = example()?;
    println!("{:?}", df);
    Ok(())
}
cargo run
   Compiling try_polars v0.1.0 (/Users/shirotabi/Project/project/Rust/try_polars)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.55s
     Running `target/debug/try_polars`
shape: (9_999, 1)
┌─────────────────────┐
│ 0.22445966924897276 │
│ ---                 │
│ f64                 │
╞═════════════════════╡
│ 0.655784            │
│ 0.93182             │
│ 0.589129            │
│ 0.194309            │
│ 0.316218            │
│ …                   │
│ 0.221614            │
│ 5.0                 │
│ 0.804628            │
│ 0.431909            │
│ 0.399103            │
└─────────────────────┘

公式ドキュメントに載っていたコードコピーして使えなくて焦った。

トランキーロ

いいなと思ったら応援しよう!