Skip to content

bytes — Byte encoding

Every example on this page starts here

import polars as pl
import polars_hash as plh

Byte encoding, on pl.Expr as .bytes.

These expressions change a value into its own bytes, so you can send the result to any hasher in nchash or chash, or write it out directly. Each one accepts Boolean, Int8/16/32/64, UInt8/16/32/64, Float32/64, Utf8 or Binary.

Each type keeps its own width: Int8 makes 1 byte, Int32 makes 4, Float64 makes 8, and so on. The two expressions differ only in the order of the bytes of that width. A value that is already bytes, Utf8 or Binary, has no byte order of its own, and it passes through unchanged either way.

Tip

This namespace encodes a value. It does not hash one. Send the result to a hasher to get a hash of the value itself, and not of a string form of it: plh.col("id").cast(pl.Int64).bytes.to_le().nchash.murmur32().

to_le

to_le() -> Expr

Encode the value as its own bytes, least significant byte first.

Returns:

Type Description
Expr

Binary, of the width of the input type. Boolean and the two 8-bit integer types write one byte, Boolean as 0x00 or 0x01. There is no second byte to order, so to_le() and to_be() agree on those three types. Utf8 writes its raw UTF-8 bytes and Binary passes through, and the byte order changes neither.

Raises:

Type Description
ComputeError

The input is a type this namespace does not accept, for example Date or Decimal. The message is expected a numeric, Boolean, String or Binary input, got `date`.

Note

To get a different width, cast first. plh.col("x").cast(pl.Int64).bytes.to_le() widens a narrower integer to 8 bytes before it encodes, sign-extended as any polars numeric cast is.

Examples:

>>> df = pl.DataFrame({"literal": [1]}, schema={"literal": pl.Int32})
>>> df.select(plh.col("literal").bytes.to_le()).item()
b'\x01\x00\x00\x00'

to_be

to_be() -> Expr

Encode the value as its own bytes, most significant byte first.

Everything on to_le() applies here, except for the order of the bytes.

Returns:

Type Description
Expr

Binary, of the width of the input type.

Raises:

Type Description
ComputeError

The input is a type this namespace does not accept.

Examples:

>>> df = pl.DataFrame({"literal": [1]}, schema={"literal": pl.Int32})
>>> df.select(plh.col("literal").bytes.to_be()).item()
b'\x00\x00\x00\x01'