I am not sure if there is an "official" way to do this since pivoting is such a common operation, but this is the way that I found digging in a bit into the tests of the Polars (I am new at it as well)... Maybe someone from Polars can help out here.
I have written in rust "as is" (the desired columns, index and values) in your Python code. You can pivot this way, but if your intention is to pivot to summarize you might be better off with groupby functions (see the examples book https://pola-rs.github.io/polars-book/user-guide/)
You need to use the external crate polars-ops
use polars_ops::pivot::{pivot, PivotAgg};
use polars::prelude::*;
fn main() {
let df = df! [
"obj" => ["ring", "shoe", "ring"],
"price" => [65, 42, 65],
"value" => [53, 55, 54],
"date" =>["2022-02-07", "2022-01-07", "2022-03-07"]
]
.unwrap();
let out = pivot(&df, ["price", "value"], ["obj"], ["date"], PivotAgg::Sum, true).unwrap();
println!("{}",df);
println!("{}",out);
}
Result
shape: (3, 4)
┌──────┬───────┬───────┬────────────┐
│ obj ┆ price ┆ value ┆ date │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i32 ┆ i32 ┆ str │
╞══════╪═══════╪═══════╪════════════╡
│ ring ┆ 65 ┆ 53 ┆ 2022-02-07 │
├╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ shoe ┆ 42 ┆ 55 ┆ 2022-01-07 │
├╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ ring ┆ 65 ┆ 54 ┆ 2022-03-07 │
└──────┴───────┴───────┴────────────┘
shape: (2, 7)
┌──────┬──────────────────┬──────────────────┬──────────────────┬──────────────────┬──────────────────┬──────────────────┐
│ obj ┆ price_2022-01-07 ┆ price_2022-02-07 ┆ price_2022-03-07 ┆ value_2022-01-07 ┆ value_2022-02-07 ┆ value_2022-03-07 │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i32 ┆ i32 ┆ i32 ┆ i32 ┆ i32 ┆ i32 │
╞══════╪══════════════════╪══════════════════╪══════════════════╪══════════════════╪══════════════════╪══════════════════╡
│ ring ┆ null ┆ 65 ┆ 65 ┆ null ┆ 53 ┆ 54 │
├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ shoe ┆ 42 ┆ null ┆ null ┆ 55 ┆ null ┆ null │
└──────┴──────────────────┴──────────────────┴──────────────────┴──────────────────┴──────────────────┴──────────────────┘