polars.lit#

polars.lit(
value: Any,
dtype: PolarsDataType | None = None,
*,
allow_object: bool = False,
) Expr[source]#

Return an expression representing a literal value.

Parameters:
value

Value that should be used as a literal.

dtype

Optionally define a dtype.

allow_object

If type is unknown use an ‘object’ type. By default, we will raise a ValueException if the type is unknown.

Notes

Expected datatypes

  • pl.lit([]) -> empty Series Float32

  • pl.lit([1, 2, 3]) -> Series Int64

  • pl.lit([[]])-> empty Series List<Null>

  • pl.lit([[1, 2, 3]]) -> Series List<i64>

  • pl.lit(None) -> Series Null

Examples

Literal scalar values:

>>> pl.lit(1)  
>>> pl.lit(5.5)  
>>> pl.lit(None)  
>>> pl.lit("foo_bar")  
>>> pl.lit(date(2021, 1, 20))  
>>> pl.lit(datetime(2023, 3, 31, 10, 30, 45))  

Literal list/Series data (1D):

>>> pl.lit([1, 2, 3])  
>>> pl.lit(pl.Series("x", [1, 2, 3]))  

Literal list/Series data (2D):

>>> pl.lit([[1, 2], [3, 4]])  
>>> pl.lit(pl.Series("y", [[1, 2], [3, 4]]))