Build a reproducible delivery-delay analysis that separates invalid timestamps, valid rare cases and route-mix effects.
Project: audit fulfillment delays without hiding the tail
Define the event contract
Create one row per shipment with stable ID, dispatch time, arrival time, route, service promise and source revision. Include 47 fixture shipments with one duplicate event, one missing arrival, one negative duration and one valid overnight delay. State whether open shipments are excluded, censored or reported separately. The profile must count each state before filtering.
Triage before summarizing
Quarantine impossible chronology and duplicate identity conflicts with reasons. Investigate the overnight delay but keep it as valid unless evidence shows a recording error. Compute mean, median, raw MAD and a threshold-exceedance rate on the valid completed set, with numerator and denominator. Compare the summary with and without a declared valid-tail exclusion only as a sensitivity result.
Check route mix
Split the analysis by established and new routes. Report counts and rates in each period, then use one fixed reference mix to compare performance. Reproduce an aggregation reversal with a controlled fixture and ensure the dashboard shows both the pooled operational result and the standardized within-route view. The reversal lesson provides a test case.
Deliver a review packet
Save raw fixture, data cutoff, code revision, validation report, quarantine ledger, summary output and a short decision memo. Include exact counts for eligible, complete, missing, invalid and valid tail records. Rerun from the frozen snapshot and compare output checksums. A reader should be able to trace the 139-minute case from source event through every summary without it silently disappearing.
Implementation
from statistics import median
def median_delay_by_route(shipments):
grouped = {}
for shipment in shipments:
arrival = shipment["arrival_minute"]
if arrival is None or arrival < shipment["dispatch_minute"]:
continue
grouped.setdefault(shipment["route"], []).append(arrival - shipment["dispatch_minute"])
return {route: median(delays) for route, delays in grouped.items()}Performance and operating cost
Grouping N shipments costs O(N) time and space; sorting route samples for medians costs O(N log N) in the worst case. Production code must also preserve rejected-row counts and reasons, which this focused summary function leaves to the validation stage.
Common Mistakes
- Do not silently drop invalid rows without a quarantine ledger.
- Do not present only a median when tail failures matter.
- Do not attribute a pooled rate change solely to process quality without checking route mix.
Read next
- Column profiles and domain constraints before analysis
- Outlier investigation: impossible values, rare events and quarantines
- Resistant summaries: median, trimmed mean and median absolute deviation
- Subgroup distributions and aggregation reversal
Continue the workflow: Integer allocation and relaxation gaps for indivisible work.
Continue the workflow: Project: audit missing delivery scans before reporting service rates.
Continue the workflow: Project: release a support breach monitoring packet.
