HP

Blog · · 2 min read

Eight data quality checks I add first to any pipeline

A short, practical list of checks that catch most data problems early, with a Pandera example and how they map to dbt tests.

Data qualityData engineering

Bad data rarely announces itself. It arrives looking normal and breaks something three steps later. In Maple, my civic data project, I use Pandera schema assertions and dbt tests, and the project runs 13 automated data quality checks. These are the kinds of checks I start with.

The list

  1. Schema. The columns I expect exist, with the right types. If a source renames a column, I want a loud failure.
  2. Not null on keys. An identifier that can be empty is not an identifier.
  3. Uniqueness of the grain. One row should mean one thing. Duplicated keys are how double counting starts.
  4. Row count sanity. Compare to the last load or the source. A table that halves overnight is not a coincidence.
  5. Accepted values. Status and category columns should only contain values I know about.
  6. Ranges. Dates are not in the future, amounts are not negative where they should not be, percentages are between 0 and 100.
  7. Referential integrity. Every foreign key points to a real row.
  8. Freshness. The newest record is recent enough for the data to be useful.

A small Pandera example

Pandera lets you describe expectations for a pandas DataFrame and validate it:

import pandera as pa

schema = pa.DataFrameSchema({
    "permit_id": pa.Column(str, unique=True, nullable=False),
    "issued_on": pa.Column("datetime64[ns]", nullable=False),
    "value": pa.Column(float, pa.Check.ge(0)),
    "status": pa.Column(str, pa.Check.isin(["issued", "closed", "cancelled"])),
})

schema.validate(df)  # raises SchemaError if any rule fails

I run this where data first enters the pipeline, so bad input stops early.

The same checks in dbt

Several of these have a dbt version: unique, not_null, accepted_values and relationships are built in. Ranges and row counts are custom tests that return the offending rows.

Where to put checks

  • At the door, where raw data arrives, so a bad file never gets in.
  • After each big transformation, such as a join, because that is where fan-out and loss happen.
  • Before the data is shown, so a dashboard is never fed by data that failed a check.

What checks cannot do

They find the problems I thought of. They do not find a rule I have not written. So when a mistake slips through, the response is to write the check that would have caught it. The list grows from real failures.