Path: blob/main/docs/source/src/rust/user-guide/concepts/expressions.rs
7889 views
fn main() -> Result<(), Box<dyn std::error::Error>> {1// --8<-- [start:df]2use chrono::prelude::*;3use polars::prelude::*;45let df: DataFrame = df!(6"name" => ["Alice Archer", "Ben Brown", "Chloe Cooper", "Daniel Donovan"],7"birthdate" => [8NaiveDate::from_ymd_opt(1997, 1, 10).unwrap(),9NaiveDate::from_ymd_opt(1985, 2, 15).unwrap(),10NaiveDate::from_ymd_opt(1983, 3, 22).unwrap(),11NaiveDate::from_ymd_opt(1981, 4, 30).unwrap(),12],13"weight" => [57.9, 72.5, 53.6, 83.1], // (kg)14"height" => [1.56, 1.77, 1.65, 1.75], // (m)15)16.unwrap();17println!("{df}");18// --8<-- [end:df]1920// --8<-- [start:select-1]21let bmi = col("weight") / col("height").pow(2);22let result = df23.clone()24.lazy()25.select([26bmi.clone().alias("bmi"),27bmi.clone().mean().alias("avg_bmi"),28lit(25).alias("ideal_max_bmi"),29])30.collect()?;31println!("{result}");32// --8<-- [end:select-1]3334// --8<-- [start:select-2]35let result = df36.clone()37.lazy()38.select([((bmi.clone() - bmi.clone().mean()) / bmi.clone().std(1)).alias("deviation")])39.collect()?;40println!("{result}");41// --8<-- [end:select-2]4243// --8<-- [start:with_columns-1]44let result = df45.clone()46.lazy()47.with_columns([48bmi.clone().alias("bmi"),49bmi.mean().alias("avg_bmi"),50lit(25).alias("ideal_max_bmi"),51])52.collect()?;53println!("{result}");54// --8<-- [end:with_columns-1]5556// --8<-- [start:filter-1]57let result = df58.clone()59.lazy()60.filter(61col("birthdate")62.is_between(63lit(NaiveDate::from_ymd_opt(1982, 12, 31).unwrap()),64lit(NaiveDate::from_ymd_opt(1996, 1, 1).unwrap()),65ClosedInterval::Both,66)67.and(col("height").gt(lit(1.7))),68)69.collect()?;70println!("{result}");71// --8<-- [end:filter-1]7273// --8<-- [start:group_by-1]74let result = df75.clone()76.lazy()77.group_by([(col("birthdate").dt().year() / lit(10) * lit(10)).alias("decade")])78.agg([col("name")])79.collect()?;80println!("{result}");81// --8<-- [end:group_by-1]8283// --8<-- [start:group_by-2]84let result = df85.clone()86.lazy()87.group_by([88(col("birthdate").dt().year() / lit(10) * lit(10)).alias("decade"),89(col("height").lt(lit(1.7)).alias("short?")),90])91.agg([col("name")])92.collect()?;93println!("{result}");94// --8<-- [end:group_by-2]9596// --8<-- [start:group_by-3]97let result = df98.clone()99.lazy()100.group_by([101(col("birthdate").dt().year() / lit(10) * lit(10)).alias("decade"),102(col("height").lt(lit(1.7)).alias("short?")),103])104.agg([105len(),106col("height").max().alias("tallest"),107cols(["weight", "height"])108.as_expr()109.mean()110.name()111.prefix("avg_"),112])113.collect()?;114println!("{result}");115// --8<-- [end:group_by-3]116117// --8<-- [start:expression-expansion-1]118let expr = (dtype_col(&DataType::Float64).as_selector().as_expr() * lit(1.1))119.name()120.suffix("*1.1");121let result = df.lazy().select([expr.clone()]).collect()?;122println!("{result}");123// --8<-- [end:expression-expansion-1]124125// --8<-- [start:expression-expansion-2]126let df2: DataFrame = df!(127"ints" => [1, 2, 3, 4],128"letters" => ["A", "B", "C", "D"],129)130.unwrap();131let result = df2.lazy().select([expr]).collect()?;132println!("{result}");133// --8<-- [end:expression-expansion-2]134135Ok(())136}137138139