polars.Series.reinterpret#

Series.reinterpret(
*,
signed: bool | None = None,
dtype: type[int | float] | PolarsDataType | None = None,
) → Series[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.

Parameters:
signed

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

dtype

DataType to reinterpret to.

Examples

>>> s = pl.Series("a", [-(2**60), -2, 3])
>>> s
shape: (3,)
Series: 'a' [i64]
[
    -1152921504606846976
    -2
    3
]
>>> s.reinterpret(signed=False)
shape: (3,)
Series: 'a' [u64]
[
    17293822569102704640
    18446744073709551614
    3
]
>>> s.reinterpret(dtype=pl.Int64)
shape: (3,)
Series: 'a' [i64]
[
        -1152921504606846976
        -2
        3
]