Skip to content

Hash

matchlab.core.hash

Utilities for hashing data and creating unique identifiers.

Classes:

  • HashMethod

    Supported hash methods for row hashing.

Functions:

  • hash_rows

    Hash each row of df over columns.

  • hash_dataframe

    Content-address df with a hash invariant to row and column order.

Attributes:

HASH_FUNC module-attribute

HASH_FUNC = sha256

HashMethod

Bases: StrEnum


              flowchart TD
              matchlab.core.hash.HashMethod[HashMethod]

              

              click matchlab.core.hash.HashMethod href "" "matchlab.core.hash.HashMethod"
            

Supported hash methods for row hashing.

Attributes:

XXH3_128 class-attribute instance-attribute

XXH3_128 = 'xxh3_128'

SHA256 class-attribute instance-attribute

SHA256 = 'sha256'

hash_rows

hash_rows(df: DataFrame, columns: list[str], method: HashMethod = XXH3_128) -> Series

Hash each row of df over columns.

Each row's hash covers both column names and values, joined with the record and unit separator symbols (, ), so a column's name is part of what gets hashed alongside its value.

hash_dataframe

hash_dataframe(df: DataFrame, method: HashMethod = XXH3_128, as_sorted_list: list[str] | None = None) -> bytes

Content-address df with a hash invariant to row and column order.

Pass as_sorted_list (2 or more column names, e.g. ["left_id", "right_id"]) to hash those columns as a sorted set instead of individually, so (1, 2) and (2, 1) hash the same. This replaces the named columns with one sorted_list column.

Combining as_sorted_list with a nullable column can null the whole row's hash input, because Polars' concat_list returns null when any input is null.

Raises:

  • ValueError

    If as_sorted_list names fewer than two columns, or a column df doesn't have.