|
| 1 | +"""Repair a DataFrame with freshdata, then validate it with GX Core. |
| 2 | +
|
| 3 | +Great Expectations is optional and remains separate from freshdata's core |
| 4 | +dependencies. Install it before running this recipe: |
| 5 | +
|
| 6 | + pip install great-expectations |
| 7 | + python examples/11_great_expectations_recipe.py |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | + |
| 12 | +import pandas as pd |
| 13 | + |
| 14 | +import freshdata as fd |
| 15 | + |
| 16 | +os.environ.setdefault("GX_ANALYTICS_ENABLED", "false") |
| 17 | + |
| 18 | +import great_expectations as gx # noqa: E402 |
| 19 | + |
| 20 | + |
| 21 | +def make_checkpoint(): |
| 22 | + """Build an in-memory checkpoint for the cleaned orders contract.""" |
| 23 | + context = gx.get_context(mode="ephemeral") |
| 24 | + context.variables.progress_bars = { |
| 25 | + "globally": False, |
| 26 | + "metric_calculations": False, |
| 27 | + } |
| 28 | + data_source = context.data_sources.add_pandas(name="freshdata_recipe") |
| 29 | + data_asset = data_source.add_dataframe_asset(name="orders") |
| 30 | + batch_definition = data_asset.add_batch_definition_whole_dataframe("whole_frame") |
| 31 | + |
| 32 | + suite = context.suites.add(gx.ExpectationSuite(name="clean_orders")) |
| 33 | + suite.add_expectation( |
| 34 | + gx.expectations.ExpectColumnValuesToBeOfType( |
| 35 | + column="order_amount", |
| 36 | + type_="float64", |
| 37 | + ) |
| 38 | + ) |
| 39 | + suite.add_expectation( |
| 40 | + gx.expectations.ExpectColumnValuesToBeOfType( |
| 41 | + column="active", |
| 42 | + type_="bool", |
| 43 | + ) |
| 44 | + ) |
| 45 | + suite.add_expectation( |
| 46 | + gx.expectations.ExpectColumnValuesToBeBetween( |
| 47 | + column="order_amount", |
| 48 | + min_value=0, |
| 49 | + ) |
| 50 | + ) |
| 51 | + |
| 52 | + validation_definition = context.validation_definitions.add( |
| 53 | + gx.ValidationDefinition( |
| 54 | + name="clean_orders_validation", |
| 55 | + data=batch_definition, |
| 56 | + suite=suite, |
| 57 | + ) |
| 58 | + ) |
| 59 | + return context.checkpoints.add( |
| 60 | + gx.Checkpoint( |
| 61 | + name="clean_orders_checkpoint", |
| 62 | + validation_definitions=[validation_definition], |
| 63 | + ) |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +def validate(checkpoint, frame: pd.DataFrame) -> bool: |
| 68 | + """Run the checkpoint against one in-memory DataFrame.""" |
| 69 | + result = checkpoint.run(batch_parameters={"dataframe": frame}) |
| 70 | + return bool(result.success) |
| 71 | + |
| 72 | + |
| 73 | +def main() -> None: |
| 74 | + raw = pd.DataFrame( |
| 75 | + { |
| 76 | + "customer_id": [1, 2, 3, 4, 5], |
| 77 | + "order_amount": ["$19.99", "$5.00", "$8.25", "$12.40", "$7.10"], |
| 78 | + "active": ["true", "false", "true", "true", "false"], |
| 79 | + } |
| 80 | + ) |
| 81 | + checkpoint = make_checkpoint() |
| 82 | + |
| 83 | + before = validate(checkpoint, raw) |
| 84 | + print(f"Before freshdata: checkpoint passed = {before}") |
| 85 | + |
| 86 | + cleaned, report = fd.clean( |
| 87 | + raw, |
| 88 | + id_columns=("customer_id",), |
| 89 | + return_report=True, |
| 90 | + ) |
| 91 | + after = validate(checkpoint, cleaned) |
| 92 | + |
| 93 | + print(f"After freshdata: checkpoint passed = {after}") |
| 94 | + print(report.summary()) |
| 95 | + |
| 96 | + assert not before, "the raw string values should fail the typed contract" |
| 97 | + assert after, "the freshdata-cleaned values should pass the checkpoint" |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + main() |
0 commit comments