polars.Series.cast#

Series.cast(
dtype: type[int | float | str | bool] | PolarsDataType,
*,
strict: bool = True,
wrap_numerical: bool = False,
) Self[source]#

Cast between data types.

Parameters:
dtype

DataType to cast to.

strict

If True invalid casts generate exceptions instead of nulls.

wrap_numerical

If True numeric casts wrap overflowing values instead of marking the cast as invalid.

Examples

>>> s = pl.Series("a", [True, False, True])
>>> s
shape: (3,)
Series: 'a' [bool]
[
    true
    false
    true
]
>>> s.cast(pl.UInt32)
shape: (3,)
Series: 'a' [u32]
[
    1
    0
    1
]