Automate a weekly data-quality check on a messy employee CSV with Python

Automate a weekly data-quality check on a messy employee CSV with Python

Every Monday an HR export lands in a shared folder. It looks fine in Excel. Then someone loads it into pandas and the phone numbers turn into floats, a joining date sits in the future, and a duplicate employee quietly inflates the headcount. I got tired of catching that by eye, so I built EazyDataFix, an open-source Python library that scores data quality and checks a file against a contract. Here's how I run that check on a small weekly employee file. The messy weekly file This extract has eight rows, including a duplicate of employee 001. Priya's email is missing the @. Her phone has only nine digits. Arun's department has a leading space and no salary. Sneha's phone is blank. Kiran's department is lowercase sales, and his last_updated date is more than a year old. Meera's joining date is 10 January 2027. Vikram's salary is n/a. emp_id,name,email,phone,department,salary,joining_date,last_updated 001,Ravi Kumar,ravi.kumar@gmail.com,9876543210,IT,45000,2023-01-15,2026-11-20 002,Priya Shah,priya.gmail.com,987654321,HR,52000,2022-11-10,2026-11-21 003,Arun Nair,arun.nair@yahoo.com,9876543210, Finance,,2021-05-18,2026-11-19 004,Sneha Reddy,sneha.reddy@gmail.com,,IT,48000,2023-08-01,2026-11-20 005,Kiran Rao,kiran.rao@gmail.com,9988776655,sales,47000,2022-03-22,2025-06-01 006,Meera Iyer,meera.iyer@gmail.com,9123456789,IT,51000,2027-01-10,2026-11-22 001,Ravi Kumar,ravi.kumar@gmail.com,9876543210,IT,45000,2023-01-15,2026-11-20 007,Vikram Das,vikram.das@outlook.com,9000011111,HR,n/a,2024-02-02,2026-11-18 Enter fullscreen mode Exit fullscreen mode Save that as weekly_hr.csv. What goes wrong in plain pandas import pandas as pd df = pd.read_csv("weekly_hr.csv") print(df[["emp_id", "phone", "salary", "email"]].head(3)) print(df[["emp_id", "phone", "salary"]].dtypes) Enter fullscreen mode Exit fullscreen mode emp_id becomes an integer, so 001 is stored as 1. phone becomes float64, so 9876543210 is stored as 9876543210.0. If I assess the file like that, EazyDataFix reports 7 invalid phone numbers — every non-blank phone fails, because "9876543210.0" is not a phone number. edf.assess("weekly_hr.csv") scores the plain load at 79.75 (grade C). The phones were fine in the CSV; the dtype was not. I load identifiers and phones as text before any check: import pandas as pd import eazydatafix as edf dtypes = { "emp_id": "string", "name": "string", "email": "string", "phone": "string", "department": "string", "salary": "float64", } df = pd.read_csv("weekly_hr.csv", dtype=dtypes) print(edf.profile(df)) Enter fullscreen mode Exit fullscreen mode The profile now types emp_id and phone as text, with 8 rows and 8 columns. n/a in salary is already a null. Score the file report = edf.assess(df) report.summary() print(report.quality.score, report.quality.grade) print(report.quality_dimensions) Enter fullscreen mode Exit fullscreen mode After the careful load, the score is 80.10 (grade B): Score : 80.10 / 100 Grade : B Completeness : 95.31% Uniqueness : 87.50% Validity : 66.67% Consistency : 61.90% Accuracy : 100.00% Timeliness : 33.33% Enter fullscreen mode Exit fullscreen mode Completeness drops because one phone and two salaries are missing. Uniqueness is 87.50% because 1 of the 8 rows is a full duplicate of employee 001. Validity is the weak spot: priya.gmail.com is not an email, and 987654321 is not a valid phone (the library expects 10–15 digits, with no leading zero). Timeliness is 33.33% because Meera's joining date is in the future, and Kiran's last_updated value of 1 June 2025 is older than the default stale threshold of 365 days. I ran this on 28 November 2026. Accuracy stays at 100%: the salaries that did parse sit inside the default salary range. I only treat some failures as action items. The check also flags "Duplicate Text" on department because IT and HR repeat. That's normal for a category column, so I ignore it. The extra duplicate-text flags on name and email are the same full-row duplicate already visible in the uniqueness score. for item in report.validations: if not item.passed and item.rule != "Duplicate Text": print(f"{item.rule} | {item.column} | {item.message}") Enter fullscreen mode Exit fullscreen mode Missing Values | phone | 1 missing value(s) found. Missing Values | salary | 2 missing value(s) found. Email Format | email | 1 invalid email address(es) found. Phone Number | phone | 1 invalid phone number(s) found. Whitespace Consistency | department | 1 value(s) contain leading/trailing whitespaces. Future Date | joining_date | 1 future date(s) found. Stale Data | last_updated | 1 stale record(s) found. Enter fullscreen mode Exit fullscreen mode The recommendations match those issues: handle missing values, remove duplicate rows, fix invalid values (no auto-fix), trim the leading space in Finance, and review the date fields (no auto-fix). report.to_markdown("quality_report.md") and report.to_json("quality_report.json") write the same findings for the week's archive. Passing a DataFrame means the report's file name shows as DataFrame. Fail the week when the contract breaks A score is useful, but I also want a pass/fail gate: required columns, types, unique employee IDs, and no negative salaries. I infer the schema from this carefully typed frame, forbid surprise columns, and add the rules I care about: base = edf.infer_schema(df) contract = edf.DataContract(fields=base.fields, allow_extra_columns=False) rules = ( edf.QualityRule("employee_id_unique", "emp_id", "unique"), edf.QualityRule("email_present", "email", "not_null"), edf.QualityRule("name_present", "name", "not_null"), edf.QualityRule("salary_not_negative", "salary", "min", 0), edf.QualityRule("salary_present", "salary", "not_null"), ) result = edf.validate_contract(df, contract, rules) print(result.passed) for check in result.results: if not check.passed: print(f"{check.name}: {check.column}: {check.message}") Enter fullscreen mode Exit fullscreen mode False employee_id_unique: emp_id: 1 duplicate value(s). salary_present: salary: 2 null value(s). Enter fullscreen mode Exit fullscreen mode The types and required columns pass. The week still fails, which is what I want: the duplicate ID and the two missing salaries should not slip through. The next week's file dropped last_updated, added a bonus column, and included a salary of -200.0. Same contract, same load dtypes: False required_column: last_updated: Column is missing. extra_column: bonus: Column is not allowed. salary_not_negative: salary: 1 value(s) below 0. Enter fullscreen mode Exit fullscreen mode I keep salary in the dtype map as float64. If I don't, a week with no missing salaries can be inferred as int64, and a contract that expects float64 fails on the type alone. The comparison is exact. A script I can schedule The edf CLI can assess a path. With {"workflow": "assess"} in assess.json, this writes a JSON summary and exits 0: edf weekly_hr.csv --config assess.json --output assess_out.json Enter fullscreen mode Exit fullscreen mode On this file the summary is "quality_score": 79.75. The CLI uses the default loader, so phones become floats again, and the assess workflow does not fail on a low score. For a weekly gate I use a short script that reuses the dtypes, contract, and rules above: import sys df = pd.read_csv("weekly_hr.csv", dtype=dtypes) report = edf.assess(df) report.to_markdown("quality_report.md") report.to_json("quality_report.json") result = edf.validate_contract(df, contract, rules) ok = result.passed and report.quality.score >= 85 print(report.quality.score, report.quality.grade, "contract", result.passed) sys.exit(0 if ok else 1) Enter fullscreen mode Exit fullscreen mode This week prints 80.1 B contract False and exits 1. I can hang that off cron or a CI job and keep the Markdown report next to the export. Cleaning is a separate decision. A dry run of edf.fix(df, edf.FixConfig(dry_run=True, protected_columns=("emp_id", "phone", "email"))) would trim Finance, drop the duplicate row, parse the dates, and fill the two missing salaries with the median (48000.0). The score would rise to 90.34 (grade A). It would not invent a valid email for Priya, pad her phone number, or move Meera's joining date. I preview that, and I only apply it when filling salary with a median is actually what I want. Try it pip install eazydatafix. The quickstart notebook walks through a smaller sales file in Colab. The code is in the GitHub repo, and the package is on PyPI. Site and docs: eazydatafix.com. If a weekly export breaks a contract in a way the rules should have caught, I'd like to hear about it.

Original Source

Read the full article at Dev →

KhanList aggregates and links to publicly available news content. We do not host full articles from third-party sources. Always verify important information with original sources.