polars.Expr.reinterpret#

Expr.reinterpret(
*,
signed: bool | None = None,
dtype: PolarsDataType | None = None,
) → Expr[source]#

Reinterpret the underlying bits as a signed/unsigned integer, float or decimal.

This operation is only allowed between numeric types of the same size. To convert between types of different sizes, use cast() instead.

A Decimal can also be reinterpreted to and from Int128, its unscaled integer representation: 1.23 as Decimal(5, 2) becomes 123. Reinterpreting an Int128 raises if the value doesn’t fit the precision of the target Decimal.

Exactly one of signed or dtype must be specified.

engine:In MemoryStreamingDistributed
Parameters:
signed

If True, reinterpret as signed integer. Otherwise, reinterpret as unsigned integer.

dtype

DataType to reinterpret to.

Examples

>>> s = pl.Series("a", [1, 1, 2], dtype=pl.UInt64)
>>> df = pl.DataFrame([s])
>>> df.select(
...     [
...         pl.col("a").reinterpret(dtype=pl.Int64).alias("reinterpreted"),
...         pl.col("a").alias("original"),
...     ]
... )
shape: (3, 2)
┌───────────────┬──────────┐
│ reinterpreted ┆ original │
│ ---           ┆ ---      │
│ i64           ┆ u64      │
╞═══════════════╪══════════╡
│ 1             ┆ 1        │
│ 1             ┆ 1        │
│ 2             ┆ 2        │
└───────────────┴──────────┘