Skip to content

Catalog.from_polars

Catalog.from_polars classmethod

from_polars(
    df: DataFrame | LazyFrame,
    token_col: str,
    corpora: Collection[str],
    normalize: bool = True,
) -> Catalog

Creates a Catalog of corpora from a Polars dataframe.

Example
import polars as pl
import wordlevel as wl

df = pl.DataFrame(
    {
        "token": ["good", "bad", "happy", "sad"],
        "before": [12, 8, 6, 4],
        "after": [5, 7, 3, 9],
    }
)

cl = wl.Catalog.from_polars(df, token_col="token", corpora=["before", "after"])

Parameters:

Name Type Description Default

df

DataFrame | LazyFrame

A Polars DataFrame or LazyFrame containing corpus information. Rows are tokens and columns are their frequencies in different corpora

required

token_col

str

The column that contains the tokens (words, subwords, n-grams, etc.)

required

corpora

Collection[str]

The columns that include the token frequencies for each corpus

required

normalize

bool

Whether to normalize the corpus frequencies for each corpus so that they are in the range \([0, 1]\) and sum to 1. The original corpus frequencies will be preserved

True

Returns:

Type Description
Catalog

A Catalog of corpora representing the provided tokens and frequencies

Raises:

Type Description
KeyError

If token_col is not a column in df.

KeyError

If any column in corpora is not a column in df.

ValueError

If fewer than two columns are provided in corpora.

Source