Validating Polars Data Frames with TDDA
Posted on Tue 01 December 2026 in TDDA • Tagged with TDDA, data validation, polars
The post Data Validation Libraries for Polars (2026 Edition), on the Posit Blog, from Rich Iannone, compares data validation of a tiny Polars DataFrame using nine different Python libraries. The DataFrame contains two values that are considered to be errors based on expectations Rich states in the post (one completely obvious; one less obvious).
The tdda library was not included in the comparison, so I thought I would
repeat his experiment using TDDA (Test-Driven Data Analysis).
The code for this blogpost is available
here on GitHub
and the tdda library and command-line tools are available
using any of the usual pip methods, e.g.
python -m pip install -U tdda
or from source at https://github.com/tdda/tdda.
Rich's Precise Constraints Without Discovery
We will start by directly implementing Rich's constraints and
verifying using the TDDA command-line tools. This is the tdda-check
target in the Makefile. The next section will show doing the same
task using the API and TDDA's constraint discovery.
The Python script create_user_data.py creates the dataset and saves
it from Polars as .parquet.
This is the input DataFrame:

The TDDA Specification in precooked/posit_constraints.tdda is as follows:
{
"fields": {
"user_id": {
"type": "int",
"sign": "positive"
},
"age": {
"type": "int",
"min": 18,
"max": 80
},
"email": {
"type": "string",
"rex": ["^[^@]+@[^@]+\\.[^@]+$"]
},
"score": {
"type": "real",
"min": 0,
"max": 100
}
}
}
The tdda detect command does detailed
validation, identifying bad values, using the
(JSON-formatted) constraints in precooked/posit_constraints.tdda.
The tdda cat command prints the output dataframe that tdda detect
writes to bads2.parquet.
Here is what happens when you run that with make tdda-check
(or by running the three commands by hand).

Rich's Test Using the TDDA API
The test Rich runs all the libraries through is to take a small Polars
DataFrame he creates, set some constraints for it, and then validate
the data to find the errors. The script below (posit.py in the repo)
performs these three tasks in TDDA, more-or-less in the way Rich does using
other libraries, but slightly more in the spirit of TDDA.
import polars as pl
from tdda.constraints.api import discover, verify, detect
def heading(line):
print(f'\n\n{line}\n{"=" * len(line)}\n')
user_data = pl.DataFrame({
"user_id": [1, 2, 3, 4, 5],
"age": [25, 30, 22, 45, 95], # <- includes a very high age
"email": [
"user1@example.com", "user2@example.com",
"invalid-email", # <- has an invalid email
"user4@example.com", "user5@example.com"
],
"score": [85.5, 92.0, 78.3, 88.7, 95.2]
})
heading('THE INPUT DATA:')
print(user_data)
constraints = discover(user_data, 'constraints.tdda', inc_rex=True)
heading('AUTO-DISCOVERED CONSTRAINTS:')
print(constraints)
constraints['age'].constraints['max'].value = 80
constraints['email'].constraints['rex'].value = [r"^[^@]+@[^@]+\.[^@]+$"]
verification = verify(user_data, constraints, dense=True)
heading('VERIFICATION WITH HAND-TIGHTENED:')
print(verification)
heading('VERIFICATION AS DATAFRAME:')
pl.Config.set_tbl_cols(-1) # force all cols to be shown
print(verification.to_dataframe())
heading('VERIFICATION OF INDIVIDUAL FAILURES AS DATAFRAME:')
detection = detect(user_data, constraints, per_constraint=True,
interleave=True, dense=True)
print(detection, end='\n\n')
bads = detection.detected()
heading('FAILING RECORDS AS DATAFRAME:')
print(bads)
Here:
- The first block just creates Rich's data in Polars and prints it.
Here's that part of the output (running the Python script directly,
or using
make tdda-apiif you havemakeavailable):

The bad email address is identified on row 3 and the relatively old age (95) is identified on row 5.
- For the other libraries, Rich constructs the constraints by hand,
but in TDDA we almost always start by getting constraints generated
automatically from either all of the data or a known-good (or
believed mostly good) subset. The
tddalibrary'sdiscoverfunction does this and returns aConstraintsobject. We have chosen also to write it to file, but we don't need to. Theinc_rexparameter asks TDDA to generate regular expression constraints for strings where possible. This is the result:

As you can see, each field gets a type, and some kind of minimum and
maximum value (which, in the case of strings, constrain length).
Numbers also get a sign constraint (which can be useful in addition
to the min and max if you remove those), and a maximum number of
nulls (zero here, since there are no nulls in any field).
Integer, string, and date fields also get a no_duplicates constraint
if there are no duplicates, which is the case here.
Floating-point fields (type real) do not.
Finally, string fields
can get either a values constraint, listing distinct strings, or
a regular expression constraint, or both (when requested).
In this case, the regular expression finder has suggested two
regular expressions because there seemed to be two different
kinds of strings. Happily, the two expressions correspond,
in this case, precisely to the invalid string and the
valid email addresses. (We are not always this lucky.)
It noticed that the email addresses
were all @example.com so included that in the regular expression.
You can see the constraints generated by TDDA on GitHub.
- Looking at the constraints, the things we clearly need to change
are the list of regular expressions for the
emailfield (we will swap in Rich's preferred one) and the maximumage. We could adjust other constraints too. Two lines in the script directly reach in and update the values for themaxconstraint onageand therexconstraint onemail. (We would usually do this by editing JSON, rather than in code, but I wanted to stick closely to Rich's approach.) If we thenverifythe data against these updated constraints, we get the following summary output (slightly denser than normal, because we setdense=True):

TDDA makes a distinction between two kinds of data validation:
-
Verification is the process of confirming that all the constraints are satisfied by all of the data. Its output focuses on which constraints are satisfied and which, if any, are not.
-
Detection is the process of identifying which particular values in which records fail to satisfy constraints.
The verify function, whose output is shown here, displays this
constraint-centric view, showing that two constraints failed:
- The max constraint on age;
- The rex (regular expression) constraint on email.
(Note, when it says 5 passes, it means five constraints
were satisfied by all the data; it's only confusing because we
are verifying a tiny dataset that happens to have five records.)
Also notice that it has two dataset-level constraints for allowed and required fields. If the data had had extra or missing fields, those would have been flagged here. We will not focus on those in this piece.
- The verification results can also be turned into a DataFrame
with the
to_dataframe()function. The result of doing so is as follows:

- The last few lines of the script do the more detailed detection of
failing values. We could jump straight to this step, i.e. we do not
need to do verification first. We can either annotate all the input
data or, as we have here (and as is the default) build a dataframe
with only failing records. The
per_constraint=Trueoption means we get a boolean for each failing constraint for each value (an_okfield), showing exactly what values failed which constraints. Theinterleave=Trueasks for fields and their validation columns to be next to each other in the DataFrame. This is the resulting data:

Now we see the two failing records, each with one failure, and the
_ok columns specifying which values have failed which constraints.
Doing it All with TDDA's Command Line
While the tdda library has an API, it is more common to use TDDA
through command-line tools. This shell script shows the main
processes, again using --dense just to compress the output slightly.
#!/bin/sh
set -x
python create_user_data.py
tdda discover --polars --rex user_data.parquet constraints.tdda
tdda verify --polars --dense user_data.parquet constraints.tdda
python edit_by_hand.py
tdda verify --polars --dense user_data.parquet constraints_tighter.tdda
tdda detect --polars --dense user_data.parquet constraints_tighter.tdda bads.parquet
tdda cat --polars bads.parquet
This is the result of running that (where the + lines are the shell
commands being executed).

Here:
- The Python script generates the data and saves it as
.parquet. - The
discovercommand writes TDDA's auto-generated constraints. - The
verifycommand checks that the data passes the autogenerated constraints, which is normally the case.1 - The
edit_by_hand.pyscript replaces what would normally be hand-editing adjustment of the generated constraints. Sometimes this is done by editing the JSON, and in other cases, it is done by adding, removing or editing data to force the constraints to be closer to what you want, e.g. removing bad data or extending the training data to push up to desired limits. - The second
verifystep shows that two constraints fail after they have been tightened for the desired age range and email pattern. - The final
detectcommand generates the annotated DataFrame containing the bad data, writing it to parquet. - The
tdda catcommand shows the bad data file as a table.
Conclusion
Data validation with TDDA is slightly different from the approach taken in most of the other libraries Rich surveyed. The two biggest differences are:
-
There is more emphasis on a machine-learning formulation, with initial constraints being discovered (generated) from either known-good data or believed-to-be-good data.
-
There is more emphasis on command-line tools, though of course, everything they can do is also available in the API.
TDDA is also broader than just data validation, with the other major components concerning testing analyses of all kinds for correctness. The reference-testing capabilities of TDDA are central to this, but there are also approaches to avoiding errors of interpretation, errors of applicability, and errors of process, all of which are more checklist-based. You can read more about data validation with constraints in TDDA in the book mentioned below, as well as all the other aspects of the approach.
Get the Book
Read the book online. It is also available from all good booksellers and all sellers of good books.
Notes
- The reason the command line tools keep needing to be told to use
Polars is that the
tddalibrary supports Pandas and Polars (as well as flat/CSV files) and Pandas is the default. You can set a config variable to tell it to prefer Polars, in which case the--polarsoptions are not needed.
-
TDDA sometimes generates constraints that are not satisfied by quite all of the data, e.g. when doing discovery over large training data, it can allow a small number of failures. ↩