Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/ci-verification-gates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,34 @@ jobs:
--cov-fail-under=40 \
-v

readme-code-blocks:
name: README Code Blocks Against Built Wheel
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: "3.12"

- name: Build the wheel
run: |
python -m pip install --upgrade pip build
python -m build --wheel --outdir dist/

- name: Install the wheel with the extra the README names
run: |
python -m venv "$RUNNER_TEMP/readme-venv"
WHEEL="$(ls "$PWD"/dist/*.whl)"
"$RUNNER_TEMP/readme-venv/bin/pip" install "freshdata-cleaner[polars] @ file://$WHEEL"

- name: Run each README python block (python -I, outside the checkout)
working-directory: ${{ runner.temp }}
run: |
"$RUNNER_TEMP/readme-venv/bin/python" -I "$GITHUB_WORKSPACE/scripts/check_readme_blocks.py" "$GITHUB_WORKSPACE/README.md"

clean-room-artifact-audit:
name: Clean-Room Wheel & Sdist Audit (/tmp Isolated Venv)
needs: guard-no-cov-bypass
Expand Down
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ Most automated data-cleaning libraries fail in production for three reasons:
## Install

```bash
pip install freshdata-cleaner
pip install "freshdata-cleaner[polars]"
```

For pandas only, `pip install freshdata-cleaner` is enough.

To install the AST codemod tool for upgrading legacy `2.x` scripts:

```bash
Expand All @@ -38,8 +40,15 @@ pip install "freshdata-cleaner[migrate]"
import polars as pl
import freshdata as fd

# 1. Point to a lazy Parquet scan (no rows loaded into memory)
lf = pl.scan_parquet("s3://bucket/raw_customer_events.parquet")
# 1. Start from a LazyFrame. In production this is a scan, for example
# lf = pl.scan_parquet("s3://bucket/raw_customer_events.parquet")
lf = pl.LazyFrame({
"Customer ID": [1, 2, 3, 4, 5, 5],
"Signup Age": [34, None, 29, 120, 41, 41],
"Segment": [" retail", "b2b ", "retail", None, "b2b", "b2b"],
"Invoice Total": [12.5, 9.0, 11.0, 10.5, 14.0, 14.0],
"churned": [0, 1, 0, 0, 1, 1],
})

# 2. Define explicit column protections and rules
config = fd.CleanConfig(
Expand All @@ -58,13 +67,17 @@ plan = fd.suggest_plan(lf, config=config)

for action in plan.actions:
print(f"[{action.stage}] {action.column}: {action.action} {action.parameters}")
# Example output (abridged) for raw columns "Customer ID", "Signup Age", "Segment", ...:
# Output:
# [column_names] Customer ID: normalize_name {'to': 'customer_id'}
# [column_names] Signup Age: normalize_name {'to': 'signup_age'}
# [missing] signup_age: impute_median {}
# [column_names] Segment: normalize_name {'to': 'segment'}
# [strings] segment: strip_strings {}
# [outliers] signup_age: clip_iqr {'lower': 27.125, 'upper': 48.125}
# [column_names] Invoice Total: normalize_name {'to': 'invoice_total'}
# [missing] invoice_total: impute_median {}
# [outliers] signup_age: clip_iqr {'lower': 27.875, 'upper': 48.875}
# [duplicates] *: drop_duplicates {}
# customer_id (id) and churned (target) get no repair actions.
# customer_id (id) and churned (target) get no imputation, stripping, or clipping.

# 4. Compile the approved plan onto the LazyFrame (zero materialization)
cleaned_lf, report = fd.apply_plan(lf, plan, return_report=True)
Expand Down Expand Up @@ -94,7 +107,7 @@ for action in diff["removed"]:
print(f"- [{action.stage}] {action.column}: {action.action}")
for change in diff["modified"]:
print(f"~ [{change['stage']}] {change['column']}: {change['before']['parameters']} -> {change['after']['parameters']}")
# + [outliers] invoice_total: clip_iqr {'lower': 7.125, 'upper': 15.125}
# + [outliers] invoice_total: clip_iqr {'lower': 6.125, 'upper': 18.125}
```

---
Expand Down
40 changes: 40 additions & 0 deletions scripts/check_readme_blocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Run each ```python block of a Markdown file, in order, in one namespace.

A later block can use names from an earlier block, as a reader does.
Blocks in other languages (bash, text) are listed and skipped.
Run with ``python -I`` so ``import freshdata`` loads the installed wheel, not src/.

python -I scripts/check_readme_blocks.py README.md
"""

from __future__ import annotations

import pathlib
import re
import sys

FENCE = re.compile(r"^```(\w*)\n(.*?)^```", re.DOTALL | re.MULTILINE)


def main(path: str) -> int:
text = pathlib.Path(path).read_text(encoding="utf-8")
namespace: dict[str, object] = {"__name__": "__main__"}
ran = 0
for block in FENCE.finditer(text):
line = text.count("\n", 0, block.start()) + 1
if block.group(1) != "python":
print(f"skip {path}:{line} ({block.group(1) or 'no language'})")
continue
print(f"run {path}:{line}")
# Pad with newlines so traceback line numbers are README line numbers.
exec(compile("\n" * line + block.group(2), path, "exec"), namespace)
ran += 1
if not ran:
print(f"error: {path} has no ```python blocks", file=sys.stderr)
return 1
print(f"ok: {ran} python block(s) ran")
return 0


if __name__ == "__main__":
sys.exit(main(sys.argv[1] if len(sys.argv) > 1 else "README.md"))
Loading