polars.Expr.str.to_integer#
- Expr.str.to_integer(*, base: int | IntoExprColumn = 10, strict: bool = True) Expr[source]#
将字符串列转换为具有基数进制的Int64列。
- Parameters:
- base
正整数或表达式,它是我们正在解析的字符串的基数。 默认值:10。
- strict
布尔值,默认=True 会将任何解析错误或溢出作为计算错误引发。 False 会静默转换为空值。
- Returns:
- Expr
数据类型
Int64的表达式。
示例
>>> df = pl.DataFrame({"bin": ["110", "101", "010", "invalid"]}) >>> df.with_columns(parsed=pl.col("bin").str.to_integer(base=2, strict=False)) shape: (4, 2) ┌─────────┬────────┐ │ bin ┆ parsed │ │ --- ┆ --- │ │ str ┆ i64 │ ╞═════════╪════════╡ │ 110 ┆ 6 │ │ 101 ┆ 5 │ │ 010 ┆ 2 │ │ invalid ┆ null │ └─────────┴────────┘
>>> df = pl.DataFrame({"hex": ["fa1e", "ff00", "cafe", None]}) >>> df.with_columns(parsed=pl.col("hex").str.to_integer(base=16, strict=True)) shape: (4, 2) ┌──────┬────────┐ │ hex ┆ parsed │ │ --- ┆ --- │ │ str ┆ i64 │ ╞══════╪════════╡ │ fa1e ┆ 64030 │ │ ff00 ┆ 65280 │ │ cafe ┆ 51966 │ │ null ┆ null │ └──────┴────────┘