polars.Expr.cast#

Expr.cast(
dtype: PolarsDataType | type[Any],
*,
strict: bool = True,
wrap_numerical: bool = False,
) Expr[source]#

在数据类型之间进行转换。

Parameters:
dtype

要转换到的数据类型。

strict

如果在谓词下推后对行的转换无效,则引发异常。 如果False,无效的转换将产生空值。

wrap_numerical

如果为True,数值转换将包裹溢出值,而不是将转换标记为无效。

示例

>>> df = pl.DataFrame(
...     {
...         "a": [1, 2, 3],
...         "b": ["4", "5", "6"],
...     }
... )
>>> df.with_columns(
...     pl.col("a").cast(pl.Float64),
...     pl.col("b").cast(pl.Int32),
... )
shape: (3, 2)
┌─────┬─────┐
│ a   ┆ b   │
│ --- ┆ --- │
│ f64 ┆ i32 │
╞═════╪═════╡
│ 1.0 ┆ 4   │
│ 2.0 ┆ 5   │
│ 3.0 ┆ 6   │
└─────┴─────┘