Skip to content

DS 251 · Module 2

Data Contracts and Quality Checks

Core question: which readings can be counted, which only observed, and which must stop until a person decides?

Start Section 1
0 / 6 completed
Sync my progressJoin a session to keep progress across devices and share it with your instructor

Info

After you join, your completed lesson numbers are sent to your instructor together with your learner code, so they can see your progress. Use the codes your instructor gave you or the email you sign in with, and do not enter your name, company secrets or other personal data. Progress is kept with the rest of that session's data.

Learning objectives

  • Write a data contract with the logical key (meter_id, interval_start), time zone, unit and interval convention
  • Apply rules Q01 to Q08 and give every finding a rule id, severity, action and reason
  • Normalise Wh to kWh and count a resent reading once while keeping its source row in the log
  • Find a missing interval, report coverage and keep the observed total labelled as partial
  • Recognise a conflict (two different valid values for one key) and hold the affected day for human review
  • Explain for F1, F2 and F3 whether the result is kept, observed only, or blocked, and why

The Data Contract

5 min

The Data Contract: What a Valid Reading Is

Learning objective: write down, before any cleaning, what makes a reading valid and how two readings are recognised as the same interval.

Data contract for meter-readings (schema esg-da-1.0)
ItemContractWhy
Logical key(meter_id, interval_start), compared after normalising the timeIdentifies one interval, whatever the message or row id
TimeISO 8601 with UTC offset; days cut in Asia/TaipeiThe same instant written two ways is still one interval
Interval15 minutes, left-closed, right-open, aligned to :00/:15/:30/:45Expected count per day is 96
Value and unitFinite, non-negative decimal; unit Wh or kWh; Wh divided by 1,000Totals are only meaningful in one unit
Meter mappingmeter_id must be in the registry with the same building_idA reading from an unknown meter cannot be assigned

Info

A contract is written by people and versioned (here esg-da-1.0 with transform kpi-transform-1.0). The code applies it; it does not invent new rules on the way.
  • The contract checks four data-quality dimensions: completeness, accuracy, consistency and timeliness
Check yourself

Two rows describe meter A01 at 2026-11-18T01:45:00+08:00 and at 2026-11-17T17:45:00Z. Under the contract, what are they?

Two rows describe meter A01 at 2026-11-18T01:45:00+08:00 and at 2026-11-17T17:45:00Z. Under the contract, what are they?

Key takeaway: A reading is identified by meter and normalised interval start; the contract says this before any code runs.

Rules, Warnings and Blocks

5 min

Rules Q01 to Q08: Warn or Block

Learning objective: know each rule, its severity and its action, and tell a warning (exclude or note, carry on) from a block (stop until a person decides).

Quality rules in the reference pipeline (kpi-transform-1.0)
RuleFindingSeverityAction
Q01Missing required field, or meter not in the registry for that buildingblockexclude
Q02Value is not a finite, non-negative decimalwarningexclude
Q03Unit is neither Wh nor kWhwarningexclude
Q04Interval not aligned to 15 minutes, or outside the stated periodswarningexclude
Q05Same key, same value (resend)warningdeduplicate
Q06Value given in Whinfonormalize
Q07Same key, different valid values (conflict)blockhold_for_review
Q08No valid reading for an expected interval (gap)warningrecord_gap

Warning

  • The row is excluded, counted once or noted
  • The day can still be reported, with its coverage
  • Example: a resend, a gap, an unknown unit

Block

  • The result cannot be decided by a rule
  • The affected day or batch is not published
  • Example: two different values for one interval
Check yourself

Why is a conflict (Q07) a block, while a gap (Q08) is only a warning?

Why is a conflict (Q07) a block, while a gap (Q08) is only a warning?

Key takeaway: Warnings keep the pipeline moving with a record; blocks stop it where only a person can decide.

Guided: Units and Resends

25 min

Guided: Wh, kWh and the Resend in F1 (then a 10-minute break)

Learning objective: run the pipeline on F1 together, see Q05 and Q06 in the quality log, and confirm that both periods are complete (15 minutes). Then take a 10-minute break.

  1. Open
    Put DS251_KPI_Pipeline.ipynb next to the CSV files and set VARIANT = 'F1'.
  2. Run steps 1 to 3
    Validate, normalise and group by (meter_id, interval_start).
  3. Read the log
    Find Q06 for R-C-0200 (1330 Wh to 1.33 kWh) and Q05 for R-B-0101-RESEND (same value as R-B-0101, counted once).
  4. Check counts
    Period B has 672 valid intervals of 672, period C has 672 of 672.
  5. Check totals
    B = 1,000.00 kWh and C = 900.00 kWh after deduplication and unit conversion.
Expected quality-log rows for F1 (columns: reading_id, rule_id, severity, action)
R-B-0101-RESEND,Q05,warning,deduplicate
R-C-0200,Q06,info,normalize

Info

If Python does not run on your machine, open the CSV in a spreadsheet: sort by interval_start, look for repeated keys, and divide any Wh value by 1,000. The rules and the expected results are the same. Break: 10 minutes after this slide.
Check yourself

A learner forgets to divide R-C-0200 by 1,000 and adds 1330 as if it were kWh. What happens to period C?

A learner forgets to divide R-C-0200 by 1,000 and adds 1330 as if it were kWh. What happens to period C?

Key takeaway: Convert units first, deduplicate on the key, and let the log show both steps.

Group Test: a Missing Interval

10 min

Group Test: a Missing Interval in F2

Learning objective: in groups, run F2, locate the gap, and report what the data can and cannot support.

  1. Run
    Set VARIANT = 'F2' and run every cell.
  2. Find the gap
    Look for the Q08 row in the quality log: which interval has no valid reading?
  3. Read the day
    Find the daily KPI row for that date: valid_intervals, coverage_ratio and quality_status.
  4. Read the period
    Write down period C valid intervals, coverage and the observed total.
  • A gap is not a zero: the interval is missing, so it is neither 0 kWh nor an estimated value
  • A partial day keeps its observed total and the status partial
  • Do not fill the gap with an average unless the method is approved and labelled as an estimate
Check yourself

On one day, a single interval out of 96 has no reading. How should that day appear in the daily KPI table?

On one day, a single interval out of 96 has no reading. How should that day appear in the daily KPI table?

Key takeaway: A gap lowers coverage; it does not turn into zero or into an invented value.

Group Test: a Conflict

10 min

Group Test: a Conflict in F3

Learning objective: in groups, run F3, find the two readings that disagree, and record why the day is blocked.

  1. Run
    Set VARIANT = 'F3' and run every cell.
  2. Find the conflict
    Look for Q07: two reading ids with the same meter and interval but different values.
  3. Read the day
    Check the daily KPI row: observed_kwh is empty and quality_status is blocked.
  4. Write the review request
    State both reading ids, both values and what a person would need to decide between them.

Resend (Q05)

  • Same key, same value
  • Counted once
  • Day can be reported

Conflict (Q07)

  • Same key, different values
  • Neither value counted
  • Day blocked until a person decides

Warning

Do not pick the larger, smaller or later value to make the table complete. Ask the meter owner or the person accountable for the data, and record the decision, who made it and when.
Check yourself

Two rows for meter A01 and the same interval show 2.10 kWh and 2.25 kWh, received at the same time. What should the pipeline do?

Two rows for meter A01 and the same interval show 2.10 kWh and 2.25 kWh, received at the same time. What should the pipeline do?

Key takeaway: Same key with different values is a conflict: keep the evidence, block the day, ask a person.

Exercise: Quality Log and Decisions

5 min

Exercise: Quality Log and Decisions for F1 to F3

Learning objective: hand in a data contract and a quality log, and state one decision per variant: keep, observe only, or block.

Decision template (fill in from your own run)
VariantRules foundPeriod C coverageDecision and reason
F1keep / observe / block
F2keep / observe / block
F3keep / observe / block

Done

This is exercise 2 (ds251-m2-quality-checks). Your contract and quality log become the data-contract and quality-rule fields of the KPI Evidence Handoff Package.

Key takeaway: Module summary: a contract and a quality log turn messy messages into counted, observed or blocked results, each with a reason.

What next?