Data contracts, monitoring and alerts in production
Data Quality Automation with Python · 7 questions
Short notes
Key idea
A data contract is a versioned agreement between data producers and consumers (schema, types, rules, freshness, owner), checked automatically on every delivery. Monitors watch production for what rules can't list: volume against history, freshness, and null-rate drift. Alerts must reach an owner with enough detail to act.
Syntax
from pandas.api import types as pt
kind = "integer" if pt.is_integer_dtype(s) else ... # compare kinds
missing = set(contract_cols) - set(df.columns)
new = set(df.columns) - set(contract_cols)
runs["trailing_avg"] = runs["rows"].shift(1).rolling(3).mean()
runs["alert"] = (runs["rows"] < 0.5 * runs["trailing_avg"]).fillna(False)
age_days = (pd.Timestamp.now() - latest).days
df.groupby("load_id")["col"].apply(lambda s: s.isna().mean())
Example
dw_orders has a load_id column its contract doesn't list (drift). The volume monitor flags August (1 row) and October (0 rows, but the source had no orders). On 20 January 2025 the data is 51 days old: stale. Missing payment methods appear only in load 5.
Traps
- Contracts not checked automatically.
- Exact dtype comparisons across pandas versions.
- Breaking changes without a major version.
- Noisy monitors; monitors instead of reconciliation.
Want the full lesson with step-by-step examples, a project and a certificate? Data Quality Automation with Python course, ₹120 →
- Question 1easy
Which of these does a data contract usually NOT contain?
- Question 2medium
Which change to a table is breaking and needs a major version and notice to consumers?
- Question 3hard
What does this print?
import pandas as pd rows = pd.Series([5, 6, 2, 3, 2, 4, 3, 1]) avg = rows.shift(1).rolling(3).mean() print(int((rows < 0.5 * avg).sum()))
- Question 4medium
The contract allows data up to 35 days old. What does this print?
import pandas as pd age = (pd.Timestamp("2025-01-20") - pd.Timestamp("2024-11-30")).days print(age > 35) - Question 5easy
Which alert message is best?
Data contracts, monitoring and alerts in production
Data contracts, monitoring and alerts in production
Data contracts, monitoring and alerts in production
Data contracts, monitoring and alerts in production
Data contracts, monitoring and alerts in production
2 more questions on this topic, with hints, and tests
Sign up free for 21 days of every question and test.