awslabs/python-deequ
Python API for Deequ
What is it?
PyDeequ is a Python library that lets you define "unit tests for data" — automated checks that measure data quality in large datasets. It's the Python API for Deequ, a library built on top of Apache Spark. So you get Deequ's power but call it from Python.
The 4 core pieces
- Metrics Computation — two sub-modules:
Profiles: analyzes every column of a dataset (think: column-by-column summary).Analyzers: the foundational module that computes metrics for both profiling and validation at scale.
- Constraint Suggestion — you specify rules for groups of analyzers, and it returns a collection of constraints it suggests you run as a Verification Suite.
- Constraint Verification — validates a dataset against constraints you set (like "size ≥ 3" or "column b is never negative"). This is the main "unit test" workflow.
- Metrics Repository — persists and tracks Deequ runs over time, so you can store results and load them back later.
How it works (quickstart)
- Install:
pip install pydeequ - Pick your Spark version: Set
SPARK_VERSIONenv var to3.5or4.1(PyDeequ 1.7.0+ tracks Deequ 2.0.21 for Spark 3.5 and 2.0.18 for Spark 4.1; for Spark 3.1–3.4 use PyDeequ 1.6.0). - Start a Spark session with the PyDeequ Maven coordinates via
spark.jars.packagesandspark.jars.excludes.
Then you use the components:
- Analyzers: run e.g.
Size()orCompleteness("b")on a DataFrame, get metrics as a DataFrame. - Profile:
ColumnProfilerRunnergives a dict of per-column profiles. - Constraint Suggestions:
ConstraintSuggestionRunnerreturns JSON of suggested constraints. - Constraint Verification: build a
Checkwith constraints like.hasSize(...),.isComplete("c"),.isUnique("a"), etc., run aVerificationSuite, and get a pass/fail results DataFrame. - Row-level results: You can also get per-row pass/fail booleans (named after the check description). If a Check has multiple constraints they're ANDed into one column. Only constraints with row-level support produce output columns.
- DQDL rules: You can write rules in Deequ's Data Quality Definition Language
Rules=[RowCount >= 3, IsComplete "a", DataFreshness "updated_at" <= 24 hours], then run viaEvaluateDataQuality.processor.processRows. For dataset comparison rules likeRowCountMatch "reference", you pass a reference DataFrame viaadditionalDataSources. - Metrics Repository: Save analysis results with
useRepository()+saveOrAppendResult()using a result key; laterrepository.load().before(...).forAnalyzers(...)to get them back. - Wrap-up: shut down the Spark session when done to avoid hanging processes.
Advanced / notes for real usage
- If you can't configure Spark session programmatically (e.g., Databricks), check vendor docs for installing JARs from Maven Central.
- The README shows how to shut down the Spark gateway:
spark.sparkContext._gateway.shutdown_callback_server()thenspark.stop().
For contributors
Developing PyDeequ locally requires: SDKMAN (for Java + Spark installs), Java (11 or 8), Apache Spark 3.5, Poetry for Python deps. Run tests with poetry run pytest, or via Docker (docker build . -t spark-3.5-docker-test && docker run spark-3.5-docker-test).
License
Apache License 2.0.
Related
- Project
- Project
- Project
- Project