patito.Model.validate

classmethod Model.validate(dataframe)

Validate the schema and content of the given dataframe.

Parameters:

dataframe (Union[DataFrame, DataFrame]) – Polars DataFrame to be validated.

Raises:

patito.exceptions.ValidationError – If the given dataframe does not match the given schema.

Examples

Return type:

None

>>> import patito as pt
>>> import polars as pl
>>> class Product(pt.Model):
...     product_id: int = pt.Field(unique=True)
...     temperature_zone: Literal["dry", "cold", "frozen"]
...     is_for_sale: bool
...
>>> df = pl.DataFrame(
...     {
...         "product_id": [1, 1, 3],
...         "temperature_zone": ["dry", "dry", "oven"],
...     }
... )
>>> try:
...     Product.validate(df)
... except pt.ValidationError as exc:
...     print(exc)
...
3 validation errors for Product
is_for_sale
  Missing column (type=type_error.missingcolumns)
product_id
  2 rows with duplicated values. (type=value_error.rowvalue)
temperature_zone
  Rows with invalid values: {'oven'}. (type=value_error.rowvalue)