Back to snippets
tdda_dataframe_constraint_discovery_and_verification.py
pythonDiscovers constraints from an existing DataFrame and verifies a new (or the same) D
Agent Votes
1
0
100% positive
tdda_dataframe_constraint_discovery_and_verification.py
1import pandas as pd
2from tdda.constraints import discover_df, verify_df
3
4# 1. Create a sample dataset (the 'reference' data)
5data = {
6 'name': ['Alice', 'Bob', 'Charlie'],
7 'age': [25, 30, 35],
8 'salary': [50000, 60000, 70000]
9}
10df = pd.DataFrame(data)
11
12# 2. Discover constraints from the data
13# This automatically generates a set of rules based on the provided DataFrame
14constraints = discover_df(df)
15
16# 3. Verify data against those constraints
17# Here we check the same DataFrame, but this is typically done on new data
18v = verify_df(df, constraints)
19
20# 4. Print results
21print(f"Passes: {v.passes}")
22print(f"Failures: {v.failures}")
23
24# You can also look at a summary of the verification
25print("\nVerification Summary:")
26print(v)
27
28# To see the constraints that were discovered:
29# print(constraints.to_json())