pub enum Expr {
Show 24 variants
Alias(Box<Expr>, Arc<str>),
Column(Arc<str>),
Columns(Vec<String>),
DtypeColumn(Vec<DataType>),
Literal(LiteralValue),
BinaryExpr {
left: Box<Expr>,
op: Operator,
right: Box<Expr>,
},
Cast {
expr: Box<Expr>,
data_type: DataType,
strict: bool,
},
Sort {
expr: Box<Expr>,
options: SortOptions,
},
Take {
expr: Box<Expr>,
idx: Box<Expr>,
},
SortBy {
expr: Box<Expr>,
by: Vec<Expr>,
reverse: Vec<bool>,
},
Agg(AggExpr),
Ternary {
predicate: Box<Expr>,
truthy: Box<Expr>,
falsy: Box<Expr>,
},
Function {
input: Vec<Expr>,
function: FunctionExpr,
options: FunctionOptions,
},
Explode(Box<Expr>),
Filter {
input: Box<Expr>,
by: Box<Expr>,
},
Window {
function: Box<Expr>,
partition_by: Vec<Expr>,
order_by: Option<Box<Expr>>,
options: WindowOptions,
},
Wildcard,
Slice {
input: Box<Expr>,
offset: Box<Expr>,
length: Box<Expr>,
},
Exclude(Box<Expr>, Vec<Excluded>),
KeepName(Box<Expr>),
Count,
Nth(i64),
RenameAlias {
function: SpecialEq<Arc<dyn RenameAliasFn>>,
expr: Box<Expr>,
},
AnonymousFunction {
input: Vec<Expr>,
function: SpecialEq<Arc<dyn SeriesUdf>>,
output_type: SpecialEq<Arc<dyn FunctionOutputField>>,
options: FunctionOptions,
},
}Expand description
Queries consists of multiple expressions.
Variants§
Alias(Box<Expr>, Arc<str>)
Column(Arc<str>)
Columns(Vec<String>)
DtypeColumn(Vec<DataType>)
Literal(LiteralValue)
BinaryExpr
Cast
Sort
Take
SortBy
Agg(AggExpr)
Ternary
A ternary operation if true then “foo” else “bar”
Function
Explode(Box<Expr>)
Filter
Window
See postgres window functions
Fields
options: WindowOptionsWildcard
Slice
Fields
Exclude(Box<Expr>, Vec<Excluded>)
Can be used in a select statement to exclude a column from selection
KeepName(Box<Expr>)
Set root name as Alias
Count
Special case that does not need columns
Nth(i64)
Take the nth column in the DataFrame
RenameAlias
AnonymousFunction
Implementations§
Source§impl Expr
impl Expr
Sourcepub fn to_dot(&self) -> Result<String, PolarsError>
pub fn to_dot(&self) -> Result<String, PolarsError>
Get a dot language representation of the Expression.
Source§impl Expr
impl Expr
Sourcepub fn is_not_null(self) -> Expr
pub fn is_not_null(self) -> Expr
Run is_not_null operation on Expr.
Sourcepub fn drop_nulls(self) -> Expr
pub fn drop_nulls(self) -> Expr
Drop null values
Sourcepub fn quantile(self, quantile: f64, interpol: QuantileInterpolOptions) -> Expr
pub fn quantile(self, quantile: f64, interpol: QuantileInterpolOptions) -> Expr
Compute the quantile per group.
Sourcepub fn agg_groups(self) -> Expr
pub fn agg_groups(self) -> Expr
Get the group indexes of the group by operation.
Sourcepub fn slice<E, F>(self, offset: E, length: F) -> Expr
pub fn slice<E, F>(self, offset: E, length: F) -> Expr
Slice the Series.
offset may be negative.
Sourcepub fn append<E>(self, other: E, upcast: bool) -> Expr
pub fn append<E>(self, other: E, upcast: bool) -> Expr
Append expressions. This is done by adding the chunks of other to this Series.
Sourcepub fn unique_stable(self) -> Expr
pub fn unique_stable(self) -> Expr
Get unique values of this expression, while maintaining order.
This requires more work than Expr::unique.
Sourcepub fn arg_unique(self) -> Expr
pub fn arg_unique(self) -> Expr
Get the first index of unique values of this expression.
Sourcepub fn arg_sort(self, sort_options: SortOptions) -> Expr
pub fn arg_sort(self, sort_options: SortOptions) -> Expr
Get the index values that would sort this expression.
Sourcepub fn strict_cast(self, data_type: DataType) -> Expr
pub fn strict_cast(self, data_type: DataType) -> Expr
Cast expression to another data type. Throws an error if conversion had overflows
Sourcepub fn sort(self, reverse: bool) -> Expr
pub fn sort(self, reverse: bool) -> Expr
Sort in increasing order. See the eager implementation.
Sourcepub fn sort_with(self, options: SortOptions) -> Expr
pub fn sort_with(self, options: SortOptions) -> Expr
Sort with given options.
Sourcepub fn map<F>(
self,
function: F,
output_type: SpecialEq<Arc<dyn FunctionOutputField>>,
) -> Expr
pub fn map<F>( self, function: F, output_type: SpecialEq<Arc<dyn FunctionOutputField>>, ) -> Expr
Apply a function/closure once the logical plan get executed.
This function is very similar to Expr::apply, but differs in how it handles aggregations.
mapshould be used for operations that are independent of groups, e.g.multiply * 2, orraise to the powerapplyshould be used for operations that work on a group of data. e.g.sum,count, etc.
It is the responsibility of the caller that the schema is correct by giving the correct output_type. If None given the output type of the input expr is used.
Sourcepub fn map_many<F>(
self,
function: F,
arguments: &[Expr],
output_type: SpecialEq<Arc<dyn FunctionOutputField>>,
) -> Expr
pub fn map_many<F>( self, function: F, arguments: &[Expr], output_type: SpecialEq<Arc<dyn FunctionOutputField>>, ) -> Expr
Sourcepub fn map_list<F>(
self,
function: F,
output_type: SpecialEq<Arc<dyn FunctionOutputField>>,
) -> Expr
pub fn map_list<F>( self, function: F, output_type: SpecialEq<Arc<dyn FunctionOutputField>>, ) -> Expr
Apply a function/closure once the logical plan get executed.
This function is very similar to apply, but differs in how it handles aggregations.
mapshould be used for operations that are independent of groups, e.g.multiply * 2, orraise to the powerapplyshould be used for operations that work on a group of data. e.g.sum,count, etc.map_listshould be used when the function expects a list aggregated series.
Sourcepub fn function_with_options<F>(
self,
function: F,
output_type: SpecialEq<Arc<dyn FunctionOutputField>>,
options: FunctionOptions,
) -> Expr
pub fn function_with_options<F>( self, function: F, output_type: SpecialEq<Arc<dyn FunctionOutputField>>, options: FunctionOptions, ) -> Expr
A function that cannot be expressed with map or apply and requires extra settings.
Sourcepub fn apply<F>(
self,
function: F,
output_type: SpecialEq<Arc<dyn FunctionOutputField>>,
) -> Expr
pub fn apply<F>( self, function: F, output_type: SpecialEq<Arc<dyn FunctionOutputField>>, ) -> Expr
Apply a function/closure over the groups. This should only be used in a groupby aggregation.
It is the responsibility of the caller that the schema is correct by giving the correct output_type. If None given the output type of the input expr is used.
This difference with map is that apply will create a separate Series per group.
mapshould be used for operations that are independent of groups, e.g.multiply * 2, orraise to the powerapplyshould be used for operations that work on a group of data. e.g.sum,count, etc.
Sourcepub fn apply_many<F>(
self,
function: F,
arguments: &[Expr],
output_type: SpecialEq<Arc<dyn FunctionOutputField>>,
) -> Expr
pub fn apply_many<F>( self, function: F, arguments: &[Expr], output_type: SpecialEq<Arc<dyn FunctionOutputField>>, ) -> Expr
Apply a function/closure over the groups with many arguments. This should only be used in a groupby aggregation.
See the Expr::apply function for the differences between map and apply.
pub fn apply_many_private( self, function_expr: FunctionExpr, arguments: &[Expr], auto_explode: bool, cast_to_supertypes: bool, ) -> Expr
pub fn map_many_private( self, function_expr: FunctionExpr, arguments: &[Expr], cast_to_supertypes: bool, ) -> Expr
Sourcepub fn is_infinite(self) -> Expr
pub fn is_infinite(self) -> Expr
Get mask of infinite values if dtype is Float
Sourcepub fn is_not_nan(self) -> Expr
pub fn is_not_nan(self) -> Expr
Get inverse mask of NaN values if dtype is Float
Sourcepub fn shift(self, periods: i64) -> Expr
pub fn shift(self, periods: i64) -> Expr
Shift the values in the array by some period. See the eager implementation.
Sourcepub fn shift_and_fill<E>(self, periods: i64, fill_value: E) -> Expr
pub fn shift_and_fill<E>(self, periods: i64, fill_value: E) -> Expr
Shift the values in the array by some period and fill the resulting empty values.
Sourcepub fn cumsum(self, reverse: bool) -> Expr
pub fn cumsum(self, reverse: bool) -> Expr
Get an array with the cumulative sum computed at every element
Sourcepub fn cumprod(self, reverse: bool) -> Expr
pub fn cumprod(self, reverse: bool) -> Expr
Get an array with the cumulative product computed at every element
Sourcepub fn cummin(self, reverse: bool) -> Expr
pub fn cummin(self, reverse: bool) -> Expr
Get an array with the cumulative min computed at every element
Sourcepub fn cummax(self, reverse: bool) -> Expr
pub fn cummax(self, reverse: bool) -> Expr
Get an array with the cumulative max computed at every element
Sourcepub fn backward_fill(self, limit: Option<u32>) -> Expr
pub fn backward_fill(self, limit: Option<u32>) -> Expr
Fill missing value with next non-null.
Sourcepub fn forward_fill(self, limit: Option<u32>) -> Expr
pub fn forward_fill(self, limit: Option<u32>) -> Expr
Fill missing value with previous non-null.
Sourcepub fn over<E, IE>(self, partition_by: E) -> Expr
pub fn over<E, IE>(self, partition_by: E) -> Expr
Apply window function over a subgroup. This is similar to a groupby + aggregation + self join. Or similar to window functions in Postgres.
§Example
#[macro_use] extern crate polars_core;
use polars_core::prelude::*;
use polars_lazy::prelude::*;
fn example() -> PolarsResult<()> {
let df = df! {
"groups" => &[1, 1, 2, 2, 1, 2, 3, 3, 1],
"values" => &[1, 2, 3, 4, 5, 6, 7, 8, 8]
}?;
let out = df
.lazy()
.select(&[
col("groups"),
sum("values").over([col("groups")]),
])
.collect()?;
dbg!(&out);
Ok(())
}
Outputs:
╭────────┬────────╮
│ groups ┆ values │
│ --- ┆ --- │
│ i32 ┆ i32 │
╞════════╪════════╡
│ 1 ┆ 16 │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 1 ┆ 16 │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 2 ┆ 13 │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 2 ┆ 13 │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ ... ┆ ... │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 1 ┆ 16 │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 2 ┆ 13 │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 3 ┆ 15 │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 3 ┆ 15 │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 1 ┆ 16 │
╰────────┴────────╯Sourcepub fn fill_nan<E>(self, fill_value: E) -> Expr
pub fn fill_nan<E>(self, fill_value: E) -> Expr
Replace the floating point NaN values by a value.
Sourcepub fn count(self) -> Expr
pub fn count(self) -> Expr
Count the values of the Series or Get counts of the group by operation.
Sourcepub fn is_duplicated(self) -> Expr
pub fn is_duplicated(self) -> Expr
Get a mask of duplicated values
pub fn xor<E>(self, expr: E) -> Expr
Sourcepub fn filter<E>(self, predicate: E) -> Expr
pub fn filter<E>(self, predicate: E) -> Expr
Filter a single column Should be used in aggregation context. If you want to filter on a DataFrame level, use LazyFrame::filter
Sourcepub fn is_in<E>(self, other: E) -> Expr
pub fn is_in<E>(self, other: E) -> Expr
Check if the values of the left expression are in the lists of the right expr.
Sourcepub fn sort_by<E, IE, R>(self, by: E, reverse: R) -> Expr
pub fn sort_by<E, IE, R>(self, by: E, reverse: R) -> Expr
Sort this column by the ordering of another column. Can also be used in a groupby context to sort the groups.
Sourcepub fn keep_name(self) -> Expr
pub fn keep_name(self) -> Expr
Keep the original root name
use polars_core::prelude::*;
use polars_lazy::prelude::*;
fn example(df: LazyFrame) -> LazyFrame {
df.select([
// even thought the alias yields a different column name,
// `keep_name` will make sure that the original column name is used
col("*").alias("foo").keep_name()
])
}Sourcepub fn map_alias<F>(self, function: F) -> Expr
pub fn map_alias<F>(self, function: F) -> Expr
Define an alias by mapping a function over the original root column name.
Sourcepub fn exclude(self, columns: impl IntoVec<String>) -> Expr
pub fn exclude(self, columns: impl IntoVec<String>) -> Expr
Exclude a column from a wildcard/regex selection.
You may also use regexes in the exclude as long as they start with ^ and end with $/
§Example
use polars_core::prelude::*;
use polars_lazy::prelude::*;
// Select all columns except foo.
fn example(df: DataFrame) -> LazyFrame {
df.lazy()
.select(&[
col("*").exclude(&["foo"])
])
}pub fn exclude_dtype<D>(self, dtypes: D) -> Expr
Sourcepub fn upper_bound(self) -> Expr
pub fn upper_bound(self) -> Expr
Get maximal value that could be hold by this dtype.
Sourcepub fn lower_bound(self) -> Expr
pub fn lower_bound(self) -> Expr
Get minimal value that could be hold by this dtype.
pub fn reshape(self, dims: &[i64]) -> Expr
Sourcepub fn shrink_dtype(self) -> Expr
pub fn shrink_dtype(self) -> Expr
Shrink numeric columns to the minimal required datatype
needed to fit the extrema of this Series.
This can be used to reduce memory pressure.
Sourcepub fn null_count(self) -> Expr
pub fn null_count(self) -> Expr
Get the null count of the column/group
Sourcepub fn set_sorted(self, sorted: IsSorted) -> Expr
pub fn set_sorted(self, sorted: IsSorted) -> Expr
Set this Series as sorted so that downstream code can use
fast paths for sorted arrays.
§Warning
This can lead to incorrect results if this Series is not sorted!!
Use with care!
pub fn str(self) -> StringNameSpace
pub fn dt(self) -> DateLikeNameSpace
pub fn arr(self) -> ListNameSpace
pub fn cat(self) -> CategoricalNameSpace
Trait Implementations§
impl Eq for Expr
Source§impl<'a> IntoIterator for &'a Expr
impl<'a> IntoIterator for &'a Expr
impl StructuralPartialEq for Expr
Auto Trait Implementations§
impl !RefUnwindSafe for Expr
impl !UnwindSafe for Expr
impl Freeze for Expr
impl Send for Expr
impl Sync for Expr
impl Unpin for Expr
impl UnsafeUnpin for Expr
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CallHasher for T
impl<T> CallHasher for T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more