import datetime
import polars as pl
import pointblank as pb
meta = pb.import_metadata(pb.load_metadata_example("datapackage.json"))
transactions = pl.DataFrame(
{
"transaction_id": ["TXN-00001", "TXN-00002", "TXN-00003", "TXN-00004"],
"customer_id": ["CUST-101", "CUST-102", "CUST-101", "CUST-103"],
"amount": [25.50, 1200.00, 0.00, 89.99],
"quantity": [1, 3, 2, 1],
"category": ["food", "electronics", "home", "toys"],
"sale_date": [datetime.date(2024, 5, d) for d in (1, 1, 2, 3)],
"discount_pct": [0.0, 10.0, 0.0, 5.0],
"email": ["a@example.com", "b@example.com", "c@example.com", "not-an-email"],
}
)
validation = meta.to_validate(data=transactions, tbl_name="transactions").interrogate()
validationto_validate()
Build a validation plan for a table from the metadata.
Usage
to_validate(
data,
**kwargs,
)This turns the declared types and constraints into validation steps, so that a table can be checked against its own documentation. The first step is a col_schema_match() with the schema from to_schema(). Then, for each variable in turn:
col_vals_not_null()if it’srequiredrows_distinct()on the column if it’suniquecol_vals_between()if it has bothmin_valandmax_val, orcol_vals_ge()orcol_vals_le()if it has only one of themcol_vals_in_set()if it hasallowed_valuescol_vals_regex()if it has apatterncol_vals_not_in_set()with itsmissing_values, if it’s a string variable that declares some
The last step checks that the declared missing-value markers don’t appear in the data, as when a file was read without converting markers like "NA" to nulls. The min_length and max_length of a variable aren’t checked. None of the steps pass null values, so a null in an optional variable fails its range, set and pattern checks. To change any of the steps, get the plan as code with to_code() and edit it.
Parameters
data: Any-
The table to validate.
**kwargs: Any-
Arguments for
Validate, such astbl_name=,thresholds=oractions=. Alabel=replaces the default label, which is"Validation: <dataset name> (<format>)", or"Validation from <format> metadata"if the metadata has no dataset name.
Returns
Validate- A Validate object with the steps added. It hasn’t been interrogated yet.
Examples
The bundled Frictionless Data Package describes a table of sales transactions. Let’s check a few transactions against it:
The metadata gave 17 steps. The table matches the declared schema, and three of the constraint checks find a failing row: the amount of 0.00 is below the minimum of 0.01, "toys" isn’t an allowed category, and "not-an-email" doesn’t match the pattern for email. To see how the plan was built, or to start from it and change some steps, print it as code:
print(meta.to_validate(data=transactions).to_code())import pointblank as pb
validation = (
pb.Validate(
data=your_data, # Replace your_data with the actual data variable
label="Validation: transactions (frictionless)",
)
.col_schema_match(schema=pb.Schema(columns=[("transaction_id", "String"), ("customer_id", "String"), ("amount", "Float64"), ("quantity", "Int64"), ("category", "String"), ("sale_date", "Date"), ("discount_pct", "Float64"), ("email", "String")]))
.col_vals_not_null(columns="transaction_id")
.rows_distinct(columns_subset="transaction_id")
.col_vals_not_in_set(columns="transaction_id", set=["NA", "N/A"])
.col_vals_not_null(columns="customer_id")
.col_vals_not_in_set(columns="customer_id", set=["NA", "N/A"])
.col_vals_not_null(columns="amount")
.col_vals_between(columns="amount", left=0.01, right=99999.99)
.col_vals_not_null(columns="quantity")
.col_vals_between(columns="quantity", left=1.0, right=1000.0)
.col_vals_not_null(columns="category")
.col_vals_in_set(columns="category", set=["electronics", "clothing", "food", "home", "sports"])
.col_vals_not_in_set(columns="category", set=["NA", "N/A"])
.col_vals_not_null(columns="sale_date")
.col_vals_between(columns="discount_pct", left=0.0, right=50.0)
.col_vals_regex(columns="email", pattern="^[^@]+@[^@]+\\.[^@]+$")
.col_vals_not_in_set(columns="email", set=["NA", "N/A"])
)
validation
Each step can be traced back to the metadata. For instance, the rows_distinct() step for transaction_id comes from its unique constraint, and the col_vals_not_in_set() steps check the "NA" and "N/A" markers declared for the string columns.