Topic 3 of 5 · 5 min read
Catching silent data errors
Goal: Add row-count and total checks that expose a dropped-row join or unit mix-up in an AI-generated analysis.
A silent data error produces a clean-looking number with no warning, so you must add checks that fail loudly: row counts, totals and a few known values. An assistant will not do this unless asked. It reports the result of the code it wrote, and it cannot see that the code lost a thousand rows on the way.
Check yourself · 1 of 3
After joining orders to customers, revenue is 8% lower than the finance report and no error appeared. What is the most likely cause?
Why it matters
Most analysis errors do not crash. They return a smaller or larger number than the truth. The four most common sources in generated code are:
- Joins that drop rows. An inner join discards every row without a match. If 8% of orders have a customer id missing from the customers table, revenue falls by about that share and nothing says so.
- Joins that multiply rows. Joining on a key that is not unique repeats the left row once per match. Totals inflate.
- Wrong grain. Averaging a column that is already an average, or summing a per-user value after joining to a per-order table.
- Units and time. Cents mixed with dollars, two currencies in one column, or UTC timestamps grouped by "day" in the wrong time zone so late-night orders land on the next date.
How to do it
- Know the expected grain. Write it down: "one row per order." After each join, check that still holds.
- Count before and after every join and filter. Print
len(left)andlen(result). - Make pandas check for you.
merge(..., validate="many_to_one")raises an error if the right side has duplicate keys.indicator=Trueadds a column showing which rows matched. - Reconcile totals. The sum of revenue after the join should equal the sum before, or the difference should be explained.
- Test against one known value. Pick a day or customer you can verify by hand or from a trusted report.
- Check units and zones. Look at min, max and a few raw values. Convert timestamps to the business time zone before extracting the date.
Before and after
Before, as the agent wrote it:
m = orders.merge(customers, on="customer_id")
print(m["amount"].sum())
After:
m = orders.merge(customers, on="customer_id", how="left", validate="many_to_one", indicator=True)
assert len(m) == len(orders), (len(orders), len(m))
print(m["_merge"].value_counts())
assert m["amount"].sum() == orders["amount"].sum()
What goes wrong
- Checking the output only. A plausible total passes a glance even when it is 8% low.
- Asking the agent "is this correct?" It will say yes. Ask it to write the checks, then run them.
- Fixing the symptom. Dropping duplicates after a fan-out join hides the wrong key instead of fixing it.
Examples
The dropped-row join
An agent reports revenue of 412,000 for November. Row counts: 9,800 orders, 9,016 after the join. The inner join to customers removed 784 orders whose customer id was missing. Switching to a left join and counting _merge == 'left_only' shows the gap, which turns out to be guest checkouts that belong in revenue.
The time-zone shift
Orders are stored in UTC and the business reports in Cairo time. df.created_at.dt.date puts an order placed at 23:30 local on the 30th into the 1st of the next month. Convert first: df.created_at.dt.tz_convert('Africa/Cairo').dt.date, then compare the monthly total to the finance report.
Key points
- Silent errors return a believable number with no warning, so add checks that fail loudly.
- An inner join drops unmatched rows and a join on a non-unique key multiplies rows.
- Count rows before and after every join and filter, and assert the grain you expect.
- Use validate and indicator in pandas merge, and reconcile totals before and after.
- Check min, max and sample values for unit and time-zone problems, and test one known value by hand.
Common mistakes
- Trusting a total because it looks plausible without comparing it to a second source.
- Using an inner join by default and never counting what it removed.
- Running drop_duplicates to make a total smaller instead of finding which join key repeated.
- Asking the assistant to confirm its own result rather than having it write assertions.
Check yourself · 2 of 3
What does merge(..., validate='many_to_one') do?
Check yourself · 3 of 3
Orders at 23:30 local time appear under the next day in a daily report. Which fix addresses the cause?
Practice items, not counted towards your mastery.
Start here
- Jupyter AI documentation (opens in a new tab)DocsFree
How the chat and magic-command assistants work in JupyterLab, and how to point them at different model providers.
- marimo reactive Python notebooks (opens in a new tab)ToolFree
A notebook that stores plain .py files and re-runs dependent cells, which removes hidden state by design.
- Ten simple rules for reproducible research in Jupyter notebooks (opens in a new tab)PaperFree
Concrete rules for ordering cells, recording dependencies and sharing notebooks that others can re-run.
More for this topic
- Merge, join, concatenate and compare (pandas) (opens in a new tab)DocsFree
Join types, the validate argument and the indicator column used to catch dropped and duplicated rows.