polars.Expr.bin.head#

Expr.bin.head(n: int | IntoExpr = 5) Expr[source]#

Take the first n bytes of the binary values.

Parameters:
n

Length of the slice (integer or expression). Negative indexing is supported; see note (2) below.

Returns:
Expr

Expression of data type Binary.

Notes

  1. A similar method exists for taking the last n bytes: tail().

  2. If n is negative, it is interpreted as “until the nth byte from the end”, e.g., head(-3) returns all but the last three bytes.

Examples

>>> colors = pl.DataFrame(
...     {
...         "name": ["black", "yellow", "blue"],
...         "code": [b"\x00\x00\x00", b"\xff\xff\x00", b"\x00\x00\xff"],
...     }
... )
>>> colors.with_columns(
...     pl.col("code").bin.head(2).alias("head"),
... )
shape: (3, 3)
┌────────┬─────────────────┬─────────────┐
│ name   ┆ code            ┆ head        │
│ ---    ┆ ---             ┆ ---         │
│ str    ┆ binary          ┆ binary      │
╞════════╪═════════════════╪═════════════╡
│ black  ┆ b"\x00\x00\x00" ┆ b"\x00\x00" │
│ yellow ┆ b"\xff\xff\x00" ┆ b"\xff\xff" │
│ blue   ┆ b"\x00\x00\xff" ┆ b"\x00\x00" │
└────────┴─────────────────┴─────────────┘