1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
//!
//! # Polars Lazy cookbook
//!
//! This page should serve a cookbook to quickly get you started with polars' query engine.
//! The lazy API allows you to create complex well performing queries on top of Polars eager.
//!
//! ## Tree Of Contents
//!
//! * [Start a lazy computation](#start-a-lazy-computation)
//! * [Filter](#filter)
//! * [Sort](#sort)
//! * [GroupBy](#groupby)
//! * [Joins](#joins)
//! * [Conditionally apply](#conditionally-apply)
//!
//! ## Start a lazy computation
//!
//! ```
//! use polars::prelude::*;
//! use polars::df;
//!
//! # fn example() -> Result<()> {
//! let df = df![
//! "a" => [1, 2, 3],
//! "b" => [None, Some("a"), Some("b")]
//! ]?;
//! // from an eager DataFrame
//! let lf: LazyFrame = df.lazy();
//!
//! // scan a csv file lazily
//! let lf: LazyFrame = LazyCsvReader::new("some_path".into())
//! .has_header(true)
//! .finish();
//!
//! // scan a parquet file lazily
//! let lf: LazyFrame = LazyFrame::scan_parquet("some_path".into(), None, true);
//!
//! # Ok(())
//! # }
//! ```
//!
//! ## Filter
//! ```
//! use polars::prelude::*;
//! use polars::df;
//!
//! # fn example() -> Result<()> {
//! let df = df![
//! "a" => [1, 2, 3],
//! "b" => [None, Some("a"), Some("b")]
//! ]?;
//!
//! let filtered = df.lazy()
//! .filter(col("a").gt(lit(2)))
//! .collect()?;
//!
//! // filtered:
//!
//! // ╭─────┬─────╮
//! // │ a ┆ b │
//! // │ --- ┆ --- │
//! // │ i64 ┆ str │
//! // ╞═════╪═════╡
//! // │ 3 ┆ "c" │
//! // ╰─────┴─────╯
//!
//! # Ok(())
//! # }
//! ```
//!
//! ## Sort
//! ```
//! use polars::prelude::*;
//! use polars::df;
//!
//! # fn example() -> Result<()> {
//! let df = df![
//! "a" => [1, 2, 3],
//! "b" => ["a", "a", "b"]
//! ]?;
//! // sort this DataFrame by multiple columns
//!
//! // ordering of the columns
//! let reverse = vec![true, false];
//!
//! let sorted = df.lazy()
//! .sort_by_exprs(vec![col("b"), col("a")], reverse)
//! .collect()?;
//!
//! // sorted:
//!
//! // ╭─────┬─────╮
//! // │ a ┆ b │
//! // │ --- ┆ --- │
//! // │ i64 ┆ str │
//! // ╞═════╪═════╡
//! // │ 1 ┆ "a" │
//! // ├╌╌╌╌╌┼╌╌╌╌╌┤
//! // │ 2 ┆ "a" │
//! // ├╌╌╌╌╌┼╌╌╌╌╌┤
//! // │ 3 ┆ "b" │
//! // ╰─────┴─────╯
//!
//! # Ok(())
//! # }
//! ```
//!
//! ## Groupby
//!
//! This example is from the polars [user guide](https://pola-rs.github.io/polars-book/user-guide/howcani/df/groupby.html).
//!
//! ```
//! use polars::prelude::*;
//! # fn example() -> Result<()> {
//!
//! let df = LazyCsvReader::new("reddit.csv".into())
//! .has_header(true)
//! .with_delimiter(b',')
//! .finish()
//! .groupby([col("comment_karma")])
//! .agg([col("name").n_unique().alias("unique_names"), col("link_karma").max()])
//! // take only 100 rows.
//! .fetch(100)?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Joins
//!
//! ```
//! use polars::prelude::*;
//! use polars::df;
//! # fn example() -> Result<()> {
//! let df_a = df![
//! "a" => [1, 2, 1, 1],
//! "b" => ["a", "b", "c", "c"],
//! "c" => [0, 1, 2, 3]
//! ]?;
//!
//! let df_b = df![
//! "foo" => [1, 1, 1],
//! "bar" => ["a", "c", "c"],
//! "ham" => ["let", "var", "const"]
//! ]?;
//!
//! let lf_a = df_a.clone().lazy();
//! let lf_b = df_b.clone().lazy();
//!
//! let joined = lf_a.join(lf_b, vec![col("a")], vec![col("foo")], JoinType::Outer).collect()?;
//! // joined:
//!
//! // ╭─────┬─────┬─────┬──────┬─────────╮
//! // │ b ┆ c ┆ a ┆ bar ┆ ham │
//! // │ --- ┆ --- ┆ --- ┆ --- ┆ --- │
//! // │ str ┆ i64 ┆ i64 ┆ str ┆ str │
//! // ╞═════╪═════╪═════╪══════╪═════════╡
//! // │ "a" ┆ 0 ┆ 1 ┆ "a" ┆ "let" │
//! // ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
//! // │ "a" ┆ 0 ┆ 1 ┆ "c" ┆ "var" │
//! // ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
//! // │ "a" ┆ 0 ┆ 1 ┆ "c" ┆ "const" │
//! // ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
//! // │ "b" ┆ 1 ┆ 2 ┆ null ┆ null │
//! // ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
//! // │ "c" ┆ 2 ┆ 1 ┆ null ┆ null │
//! // ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
//! // │ "c" ┆ 3 ┆ 1 ┆ null ┆ null │
//! // ╰─────┴─────┴─────┴──────┴─────────╯
//!
//! // other join syntax options
//! # let lf_a = df_a.clone().lazy();
//! # let lf_b = df_b.clone().lazy();
//! let inner = lf_a.inner_join(lf_b, col("a"), col("foo")).collect()?;
//!
//! # let lf_a = df_a.clone().lazy();
//! # let lf_b = df_b.clone().lazy();
//! let left = lf_a.left_join(lf_b, col("a"), col("foo")).collect()?;
//!
//! # let lf_a = df_a.clone().lazy();
//! # let lf_b = df_b.clone().lazy();
//! let outer = lf_a.outer_join(lf_b, col("a"), col("foo")).collect()?;
//!
//! # let lf_a = df_a.clone().lazy();
//! # let lf_b = df_b.clone().lazy();
//! let joined_with_builder = lf_a.join_builder()
//! .with(lf_b)
//! .left_on(vec![col("a")])
//! .right_on(vec![col("foo")])
//! .how(JoinType::Inner)
//! .force_parallel(true)
//! .finish()
//! .collect()?;
//!
//! # Ok(())
//! # }
//! ```
//!
//! ## Conditionally apply
//! If we want to create a new column based on some condition, we can use the `.when()/.then()/.otherwise()` expressions.
//!
//! * `when` - accepts a predicate epxression
//! * `then` - expression to use when `predicate == true`
//! * `otherwise` - expression to use when `predicate == false`
//!
//! ```
//! use polars::prelude::*;
//! use polars::df;
//! # fn example() -> Result<()> {
//! let df = df![
//! "range" => [1, 2, 3, 4, 5, 6, 8, 9, 10],
//! "left" => (0..10).map(|_| Some("foo")).collect::<Vec<_>>(),
//! "right" => (0..10).map(|_| Some("bar")).collect::<Vec<_>>()
//! ]?;
//!
//! let new = df.lazy()
//! .with_column(when(col("range").gt_eq(lit(5)))
//! .then(col("left"))
//! .otherwise(col("right")).alias("foo_or_bar")
//! ).collect()?;
//!
//! // new:
//!
//! // ╭───────┬───────┬───────┬────────────╮
//! // │ range ┆ left ┆ right ┆ foo_or_bar │
//! // │ --- ┆ --- ┆ --- ┆ --- │
//! // │ i64 ┆ str ┆ str ┆ str │
//! // ╞═══════╪═══════╪═══════╪════════════╡
//! // │ 0 ┆ "foo" ┆ "bar" ┆ "bar" │
//! // ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
//! // │ 1 ┆ "foo" ┆ "bar" ┆ "bar" │
//! // ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
//! // │ 2 ┆ "foo" ┆ "bar" ┆ "bar" │
//! // ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
//! // │ 3 ┆ "foo" ┆ "bar" ┆ "bar" │
//! // ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
//! // │ ... ┆ ... ┆ ... ┆ ... │
//! // ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
//! // │ 5 ┆ "foo" ┆ "bar" ┆ "foo" │
//! // ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
//! // │ 6 ┆ "foo" ┆ "bar" ┆ "foo" │
//! // ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
//! // │ 7 ┆ "foo" ┆ "bar" ┆ "foo" │
//! // ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
//! // │ 8 ┆ "foo" ┆ "bar" ┆ "foo" │
//! // ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
//! // │ 9 ┆ "foo" ┆ "bar" ┆ "foo" │
//! // ╰───────┴───────┴───────┴────────────╯
//!
//! # Ok(())
//! # }
//! ```