polars.Expr.to_physical#

Expr.to_physical() Self[source]#

Cast to physical representation of the logical dtype.

  • polars.datatypes.Date() -> polars.datatypes.Int32()

  • polars.datatypes.Datetime() -> polars.datatypes.Int64()

  • polars.datatypes.Time() -> polars.datatypes.Int64()

  • polars.datatypes.Duration() -> polars.datatypes.Int64()

  • polars.datatypes.Categorical() -> polars.datatypes.UInt32()

  • List(inner) -> List(physical of inner)

Other data types will be left unchanged.

Examples

Replicating the pandas pd.factorize function.

>>> pl.DataFrame({"vals": ["a", "x", None, "a"]}).with_columns(
...     [
...         pl.col("vals").cast(pl.Categorical),
...         pl.col("vals")
...         .cast(pl.Categorical)
...         .to_physical()
...         .alias("vals_physical"),
...     ]
... )
shape: (4, 2)
┌──────┬───────────────┐
│ vals ┆ vals_physical │
│ ---  ┆ ---           │
│ cat  ┆ u32           │
╞══════╪═══════════════╡
│ a    ┆ 0             │
│ x    ┆ 1             │
│ null ┆ null          │
│ a    ┆ 0             │
└──────┴───────────────┘