You can hold assumptions such as “this column must contain no nulls” and “this value should fall between 0 and 120” in your head. A pipeline change can break them without raising an error. Great Expectations lets you write the conditions your data must satisfy as explicit code and validate them against real data. I installed it, blocked network access, and checked validation.
Great Expectations calls each data requirement an “Expectation.” Great Expectations lets you define each requirement in code, validate it against a data frame, and report the result. The project publishes its source under the Apache 2.0 License(*1).
1. Install it
FROM docker.io/library/python:3.12.13-slim
RUN pip install --no-cache-dir --quiet 'great_expectations==1.19.0' 'pandas==2.3.3'
WORKDIR /work
podman build -t great-expectations-sandbox .
2. Add invalid data and validate it
I prepared a data frame containing three kinds of invalid data: a null user ID, an out-of-range age, and a malformed email address. I defined the matching Expectations, blocked network access, and ran the validation.
import pandas as pd
import great_expectations as gx
df = pd.DataFrame({
"user_id": [1, 2, 3, 4, 5, None],
"age": [25, 34, -3, 41, 150, 29],
"email": ["a@example.com", "b@example.com", "not-an-email", "d@example.com", "e@example.com", "f@example.com"],
})
context = gx.get_context(mode="ephemeral")
data_source = context.data_sources.add_pandas("demo_source")
data_asset = data_source.add_dataframe_asset(name="demo_asset")
batch_definition = data_asset.add_batch_definition_whole_dataframe("demo_batch")
batch = batch_definition.get_batch(batch_parameters={"dataframe": df})
suite = gx.ExpectationSuite(name="demo_suite")
suite.add_expectation(gx.expectations.ExpectColumnValuesToNotBeNull(column="user_id"))
suite.add_expectation(gx.expectations.ExpectColumnValuesToBeBetween(column="age", min_value=0, max_value=120))
suite.add_expectation(
gx.expectations.ExpectColumnValuesToMatchRegex(column="email", regex=r"^[^@]+@[^@]+\.[^@]+$")
)
results = batch.validate(suite)
for result in results.results:
print(result.expectation_config.type, "success:", result.success, "unexpected:", result.result.get("unexpected_count"))
podman run --rm --network none --memory 2g --cpus 2 --pids-limit 512 \
-v "$(pwd)":/work -w /work great-expectations-sandbox \
python validate_demo.py
The -v option lets the container read from and write to the specified directory. Use an empty directory for this experiment.
The command produced the following output.
expect_column_values_to_not_be_null: success=False unexpected_count=1
expect_column_values_to_be_between: success=False unexpected_count=2
expect_column_values_to_match_regex: success=False unexpected_count=1
overall success: False
The run detected all three invalid-data cases. It found one null user ID, two out-of-range ages (-3 and 150), and one malformed email address. The counts matched the expectations. The overall validation result was False, as expected.
Keep data assumptions explicit
If you do not write down an assumption such as “this column should contain this kind of value,” the next person who touches the pipeline will not know it. Writing that assumption as code with Great Expectations makes a failed validation visible when the data violates it. You can separate data-quality checks from manual checks that depend on one person.
Sources
*1 Great Expectations (GitHub), https://github.com/great-expectations/great_expectations, Apache 2.0 License