polars.Series.str.to_integer#

Series.str.to_integer(
*,
base: int | IntoExprColumn = 10,
dtype: PolarsIntegerType = Int64,
strict: bool = True,
) Series[source]#

Convert an String column into a column of dtype with base radix.

Parameters:
base

Positive integer or expression which is the base of the string we are parsing. Default: 10.

dtype

Polars integer type to cast to. Default: Int64.

strict

Bool, Default=True will raise any ParseError or overflow as ComputeError. False silently convert to Null.

Returns:
Series

Series of data.

Examples

>>> s = pl.Series("bin", ["110", "101", "010", "invalid"])
>>> s.str.to_integer(base=2, dtype=pl.Int32, strict=False)
shape: (4,)
Series: 'bin' [i32]
[
        6
        5
        2
        null
]
>>> s = pl.Series("hex", ["fa1e", "ff00", "cafe", None])
>>> s.str.to_integer(base=16)
shape: (4,)
Series: 'hex' [i64]
[
        64030
        65280
        51966
        null
]