Path: blob/main/docs/source/src/rust/user-guide/expressions/column-selections.rs
7889 views
use polars::prelude::*;12fn main() -> Result<(), Box<dyn std::error::Error>> {3// --8<-- [start:selectors_df]45use chrono::prelude::*;6use polars::time::*;78let df = df!(9"id" => &[9, 4, 2],10"place" => &["Mars", "Earth", "Saturn"],11"date" => date_range("date".into(),12NaiveDate::from_ymd_opt(2022, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap(), NaiveDate::from_ymd_opt(2022, 1, 3).unwrap().and_hms_opt(0, 0, 0).unwrap(), Duration::parse("1d"),ClosedWindow::Both, TimeUnit::Milliseconds, None)?,13"sales" => &[33.4, 2142134.1, 44.7],14"has_people" => &[false, true, false],15"logged_at" => date_range("logged_at".into(),16NaiveDate::from_ymd_opt(2022, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap(), NaiveDate::from_ymd_opt(2022, 1, 1).unwrap().and_hms_opt(0, 0, 2).unwrap(), Duration::parse("1s"),ClosedWindow::Both, TimeUnit::Milliseconds, None)?,17)?18.with_row_index("index".into(), None)?;19println!("{}", &df);20// --8<-- [end:selectors_df]2122// --8<-- [start:all]23let out = df.clone().lazy().select([col("*")]).collect()?;24println!("{}", &out);2526// Is equivalent to27let out = df.clone().lazy().select([all().as_expr()]).collect()?;28println!("{}", &out);29// --8<-- [end:all]3031// --8<-- [start:exclude]32let out = df33.clone()34.lazy()35.select([all().exclude_cols(["logged_at", "index"]).as_expr()])36.collect()?;37println!("{}", &out);38// --8<-- [end:exclude]3940// --8<-- [start:expansion_by_names]41let out = df42.clone()43.lazy()44.select([cols(["date", "logged_at"])45.as_expr()46.dt()47.to_string("%Y-%h-%d")])48.collect()?;49println!("{}", &out);50// --8<-- [end:expansion_by_names]5152// --8<-- [start:expansion_by_regex]53let out = df.clone().lazy().select([col("^.*(as|sa).*$")]).collect()?;54println!("{}", &out);55// --8<-- [end:expansion_by_regex]5657// --8<-- [start:expansion_by_dtype]58let out = df59.lazy()60.select([61dtype_cols([DataType::Int64, DataType::UInt32, DataType::Boolean])62.as_selector()63.as_expr()64.n_unique(),65])66.collect()?;67// gives different result than python as the id col is i32 in rust68println!("{}", &out);69// --8<-- [end:expansion_by_dtype]7071// --8<-- [start:selectors_intro]72// Not available in Rust, refer the following link73// https://github.com/pola-rs/polars/issues/1059474// --8<-- [end:selectors_intro]7576// --8<-- [start:selectors_diff]77// Not available in Rust, refer the following link78// https://github.com/pola-rs/polars/issues/1059479// --8<-- [end:selectors_diff]8081// --8<-- [start:selectors_union]82// Not available in Rust, refer the following link83// https://github.com/pola-rs/polars/issues/1059484// --8<-- [end:selectors_union]8586// --8<-- [start:selectors_by_name]87// Not available in Rust, refer the following link88// https://github.com/pola-rs/polars/issues/1059489// --8<-- [end:selectors_by_name]9091// --8<-- [start:selectors_to_expr]92// Not available in Rust, refer the following link93// https://github.com/pola-rs/polars/issues/1059494// --8<-- [end:selectors_to_expr]9596// --8<-- [start:selectors_is_selector_utility]97// Not available in Rust, refer the following link98// https://github.com/pola-rs/polars/issues/1059499// --8<-- [end:selectors_is_selector_utility]100101// --8<-- [start:selectors_colnames_utility]102// Not available in Rust, refer the following link103// https://github.com/pola-rs/polars/issues/10594104// --8<-- [end:selectors_colnames_utility]105Ok(())106}107108109