Guides
Expressions and data types
The type of an expression holds its code. When the expression meets a frame, the checker computes its column from that code and the data types of the frame, with the typing rules of Polars.
The checker computes each result
Section titled “The checker computes each result”Revealed type
TypedDataFrame[TypedDict({'price': Float64, 'symbol': UInt32, 'buy_price': Float64}, closed=True), None]pl.col("price") * 2keeps the nameprice, and gives aFloat64.str.len_chars()gives aUInt32.when/then/otherwisegives the common data type of its branches. The literal0becomes aFloat64next toprice.
The same expression can give other data types in other frames, because the checker computes it at the data types of each frame.
Aggregations
Section titled “Aggregations”An aggregation, for example sum(), gives one value for a group of rows. Its frame decides what that means:
Revealed type
TypedDataFrame[TypedDict({'quantity': Int64}, closed=True), None]Revealed type
TypedDataFrame[TypedDict({'symbol': String, 'price': Float64, 'quantity': Int64, 'total': Int64}, closed=True), None]Revealed type
TypedDataFrame[TypedDict({'symbol': String, 'quantity': Int64}, closed=True), None]- In
select, the sum gives a frame of one row. - In
with_columns, Polars repeats the sum on each row. - In
agg, the sum gives one value for each group.
A declaration can state that an expression works row by row, and the checker then refuses an aggregation. See Declare expression types.
Faults in an expression
Section titled “Faults in an expression”If Polars refuses an expression at the data types of the frame, the checker reports it:
roundneeds a number.- Polars cannot cast a
Datetimeto aBoolean. - Polars cannot add a
Datetimeto aString.
An expression that Polars refuses at every data type is an error where you write it, before it meets a frame.