Skip to content

Commit

Permalink
remaining_examples
Browse files Browse the repository at this point in the history
  • Loading branch information
deanm0000 committed Jan 22, 2025
1 parent 065097e commit 0c5ebb4
Showing 1 changed file with 162 additions and 16 deletions.
178 changes: 162 additions & 16 deletions docs/source/src/rust/user-guide/expressions/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,23 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// --8<-- [start:array-example]
use polars::prelude::*;
let df = DataFrame::new(vec![
Series::new("bit_flags".into(), [
true, true, true, true, false, false, true, true, true, true,
])
Series::new(
"bit_flags".into(),
[true, true, true, true, false, false, true, true, true, true],
)
.reshape_array(&[
ReshapeDimension::Infer,
ReshapeDimension::Specified(Dimension::new(5)),
])
.unwrap()
.into(),
Series::new("tic_tac_toe".into(), [
" ", "x", "o", " ", "x", " ", "o", "x", " ", "o", "x", "x", " ", "o", "x", " ", " ",
"o",
])
Series::new(
"tic_tac_toe".into(),
[
" ", "x", "o", " ", "x", " ", "o", "x", " ", "o", "x", "x", " ", "o", "x", " ",
" ", "o",
],
)
.reshape_array(&[
ReshapeDimension::Infer,
ReshapeDimension::Specified(Dimension::new(3)),
Expand All @@ -97,39 +101,181 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// --8<-- [end:numpy-array-inference]

// --8<-- [start:weather]
// Contribute the Rust translation of the Python example by opening a PR.
use polars::prelude::*;
let station: Vec<String> = (1..6)
.into_iter()
.map(|idx| format!("Station {}", idx))
.collect();
let weather = DataFrame::new(vec![
Series::new("station".into(), station).into(),
Series::new(
"temperature".into(),
[
"20 5 5 E1 7 13 19 9 6 20",
"18 8 16 11 23 E2 8 E2 E2 E2 90 70 40",
"19 24 E9 16 6 12 10 22",
"E2 E0 15 7 8 10 E1 24 17 13 6",
"14 8 E0 16 22 24 E1",
],
)
.into(),
])
.unwrap();
eprintln!("{:?}", weather);
// --8<-- [end:weather]

// --8<-- [start:split]
// Contribute the Rust translation of the Python example by opening a PR.
let weather = weather
.lazy()
.with_columns(vec![col("temperature").str().split(lit(" "))])
.collect()
.unwrap();
eprintln!("{:?}", weather);
// --8<-- [end:split]

// --8<-- [start:explode]
// Contribute the Rust translation of the Python example by opening a PR.
let result = weather.explode(vec!["temperature"]).unwrap();
eprintln!("{:?}", result);
// --8<-- [end:explode]

// --8<-- [start:list-slicing]
// Contribute the Rust translation of the Python example by opening a PR.
let result = weather
.clone()
.lazy()
.with_columns(vec![
col("temperature").list().head(lit(3)).alias("head"),
col("temperature").list().tail(lit(3)).alias("tail"),
col("temperature")
.list()
.slice(lit(-3), lit(2))
.alias("two_next_to_last"),
])
.collect()
.unwrap();
eprintln!("{:?}", result);
// --8<-- [end:list-slicing]

// --8<-- [start:element-wise-casting]
// Contribute the Rust translation of the Python example by opening a PR.
let result = weather
.clone()
.lazy()
.with_columns(vec![col("temperature")
.list()
// needs feature "list_eval"
.eval(col("").cast(DataType::Int64).is_null(), true)
.list()
.sum()
.alias("errors")])
.collect()
.unwrap();
eprintln!("{:?}", result);
// --8<-- [end:element-wise-casting]

// --8<-- [start:element-wise-regex]
// Contribute the Rust translation of the Python example by opening a PR.
let result2 = weather
.clone()
.lazy()
.with_columns(vec![col("temperature")
.list()
.eval(col("").str().contains(lit("(?i)[a-z]"), true), true)
.list()
.sum()
.alias("errors")])
.collect()
.unwrap();

eprintln!("{}", result.equals(&result2));
// --8<-- [end:element-wise-regex]

// --8<-- [start:weather_by_day]
// Contribute the Rust translation of the Python example by opening a PR.
let station: Vec<String> = (1..11)
.into_iter()
.map(|idx| format!("Station {}", idx))
.collect();
let weather_by_day = DataFrame::new(vec![
Series::new("station".into(), station).into(),
Series::new("day_1".into(), [17, 11, 8, 22, 9, 21, 20, 8, 8, 17]).into(),
Series::new("day_2".into(), [15, 11, 10, 8, 7, 14, 18, 21, 15, 13]).into(),
Series::new("day_3".into(), [16, 15, 24, 24, 8, 23, 19, 23, 16, 10]).into(),
])
.unwrap();

eprintln!("{}", weather_by_day);
// --8<-- [end:weather_by_day]

// --8<-- [start:rank_pct]
// Contribute the Rust translation of the Python example by opening a PR.
// needs feature "rank", "round_series"
let rank_pct = (col("")
.rank(
RankOptions {
descending: true,
..Default::default()
},
None,
)
// explicit cast here is necessary or else result is u32
.cast(DataType::Float64)
/ col("*").count())
.round(2);
use polars_plan::dsl;
let result = weather_by_day
.lazy()
.with_columns(vec![dsl::concat_list(vec![
col("*").exclude(vec!["station"])
])
.unwrap()
.alias("all_temps")])
.select(vec![
col("*").exclude(vec!["all_temps"]),
col("all_temps")
.list()
.eval(rank_pct, true)
.alias("temps_rank"),
])
.collect()
.unwrap();
eprintln!("{}", result);
// --8<-- [end:rank_pct]

// --8<-- [start:array-overview]
// Contribute the Rust translation of the Python example by opening a PR.
let df = DataFrame::new(vec![
Series::new(
"first_last".into(),
[
"Anne", "Adams", "Brandon", "Branson", "Camila", "Campbell", "Dennis", "Doyle",
],
)
.reshape_array(&[
ReshapeDimension::Infer,
ReshapeDimension::Specified(Dimension::new(2)),
])
.unwrap()
.into(),
Series::new(
"fav_numbers".into(),
[42, 0, 1, 2, 3, 5, 13, 21, 34, 73, 3, 7],
)
.reshape_array(&[
ReshapeDimension::Infer,
ReshapeDimension::Specified(Dimension::new(3)),
])
.unwrap()
.into(),
])
.unwrap();

let result = df
.lazy()
.select(vec![
col("first_last").arr().join(lit(" "), true).alias("name"),
col("fav_numbers").arr().sort(SortOptions::default()),
col("fav_numbers").arr().max().alias("largest_fav"),
col("fav_numbers").arr().sum().alias("summed"),
col("fav_numbers").arr().contains(3).alias("likes_3"),
])
.collect()
.unwrap();
eprintln!("{}", result);
// --8<-- [end:array-overview]

Ok(())
Expand Down

0 comments on commit 0c5ebb4

Please sign in to comment.