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

  1. 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.
  2. Constraint Suggestion — you specify rules for groups of analyzers, and it returns a collection of constraints it suggests you run as a Verification Suite.
  3. 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.
  4. 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_VERSION env var to 3.5 or 4.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.packages and spark.jars.excludes.

Then you use the components:

  • Analyzers: run e.g. Size() or Completeness("b") on a DataFrame, get metrics as a DataFrame.
  • Profile: ColumnProfilerRunner gives a dict of per-column profiles.
  • Constraint Suggestions: ConstraintSuggestionRunner returns JSON of suggested constraints.
  • Constraint Verification: build a Check with constraints like .hasSize(...), .isComplete("c"), .isUnique("a"), etc., run a VerificationSuite, 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 via EvaluateDataQuality.process or .processRows. For dataset comparison rules like RowCountMatch "reference", you pass a reference DataFrame via additionalDataSources.
  • Metrics Repository: Save analysis results with useRepository() + saveOrAppendResult() using a result key; later repository.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() then spark.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