polars.Expr.bin.contains#

Expr.bin.contains(literal: bytes) Expr[source]#

Check if binaries in Series contain a binary substring.

Parameters:
literal

The binary substring to look for

Returns:
Expr

Expression of data type Boolean.

See also

starts_with

Check if the binary substring exists at the start

ends_with

Check if the binary substring exists at the end

Examples

>>> colors = pl.DataFrame(
...     {
...         "name": ["black", "yellow", "blue"],
...         "code": [b"\x00\x00\x00", b"\xff\xff\x00", b"\x00\x00\xff"],
...     }
... )
>>> colors.select(
...     "name",
...     pl.col("code").bin.encode("hex").alias("code_encoded_hex"),
...     pl.col("code").bin.contains(b"\xff").alias("contains_ff"),
...     pl.col("code").bin.starts_with(b"\xff").alias("starts_with_ff"),
...     pl.col("code").bin.ends_with(b"\xff").alias("ends_with_ff"),
... )
shape: (3, 5)
┌────────┬──────────────────┬─────────────┬────────────────┬──────────────┐
│ name   ┆ code_encoded_hex ┆ contains_ff ┆ starts_with_ff ┆ ends_with_ff │
│ ---    ┆ ---              ┆ ---         ┆ ---            ┆ ---          │
│ str    ┆ str              ┆ bool        ┆ bool           ┆ bool         │
╞════════╪══════════════════╪═════════════╪════════════════╪══════════════╡
│ black  ┆ 000000           ┆ false       ┆ false          ┆ false        │
│ yellow ┆ ffff00           ┆ true        ┆ true           ┆ false        │
│ blue   ┆ 0000ff           ┆ true        ┆ false          ┆ true         │
└────────┴──────────────────┴─────────────┴────────────────┴──────────────┘