Reinterpret the underlying bits of a same-size numeric type.
Description
This operation is only allowed between numeric types of the same size.
To convert between types of different sizes, use
<expr>$cast() instead.
Usage
<Expr>$reinterpret(..., signed = NULL, dtype = NULL)
Arguments
…
|
These dots are for future extensions and must be empty. |
signed
|
Whether to reinterpret as a signed or unsigned integer of the same width
as the input. Must specify exactly one of signed and
dtype.
|
dtype
|
Target data type for the reinterpretation. Must specify exactly one of
signed and dtype.
|
Details
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 an error if
the value doesn’t fit the precision of the target Decimal.
Value
A polars expression
Examples
library("polars")
df <- pl$DataFrame(a = c(1, 1, 2))$cast(pl$UInt64)
# Reinterpret column a as Int64
df$with_columns(
reinterpreted = pl$col("a")$reinterpret(dtype = pl$Int64)
)
#> shape: (3, 2)
#> ┌─────┬───────────────┐
#> │ a ┆ reinterpreted │
#> │ --- ┆ --- │
#> │ u64 ┆ i64 │
#> ╞═════╪═══════════════╡
#> │ 1 ┆ 1 │
#> │ 1 ┆ 1 │
#> │ 2 ┆ 2 │
#> └─────┴───────────────┘
# Reinterpret a Decimal as its unscaled Int128 representation
df2 <- pl$DataFrame(a = c(1.23, -4.5))$cast(pl$Decimal(5, 2))
df2$with_columns(
reinterpreted = pl$col("a")$reinterpret(dtype = pl$Int128)
)
#> shape: (2, 2)
#> ┌──────────────┬───────────────┐
#> │ a ┆ reinterpreted │
#> │ --- ┆ --- │
#> │ decimal[5,2] ┆ i128 │
#> ╞══════════════╪═══════════════╡
#> │ 1.23 ┆ 123 │
#> │ -4.50 ┆ -450 │
#> └──────────────┴───────────────┘