From 2cde9839a55d28505c96d45b988136be0745ae1e Mon Sep 17 00:00:00 2001 From: Johnny Wilson Dougherty Date: Tue, 1 Dec 2026 15:53:11 +0530 Subject: [PATCH] fix: warn and leave skipped parse_currency out of the report When apply_plan replays a parse_currency step on data with a value that is not a currency string, it skips the column (#538). The CleanReport still listed the step as applied and nothing told the user. Now both backends warn with the column name, and the report leaves the step out. --- CHANGELOG.md | 7 +++++++ src/freshdata/_core/backends.py | 14 ++++++++++++++ src/freshdata/_core/engine.py | 6 ++++-- tests/test_skipped_currency_report.py | 25 +++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tests/test_skipped_currency_report.py diff --git a/CHANGELOG.md b/CHANGELOG.md index f096589..b81baa3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,13 @@ adheres to [Semantic Versioning](https://semver.org/). stays `"REF-9921"` instead of becoming `-9921.0`. On a Polars LazyFrame, this check runs one query on the parse_currency columns inside `apply_plan`. +### Changed + +- When `apply_plan` skips a replayed `parse_currency` step, it now warns + (`UserWarning`, naming the column), and `CleanReport.actions` leaves the step + out. Before, the report listed the step as applied although the column was + unchanged. + ## [3.0.0] - 2026-11-28 3.0 replaces the 2.x API with a small plan-and-apply core for pandas and diff --git a/src/freshdata/_core/backends.py b/src/freshdata/_core/backends.py index c140c7d..30eed55 100644 --- a/src/freshdata/_core/backends.py +++ b/src/freshdata/_core/backends.py @@ -42,6 +42,16 @@ def _reject(a: ActionRecord, cfg: CleanConfig, backend: str, reason: str = "unkn raise ValueError(f"{msg}. Fix the plan, or set CleanConfig(strict_backend=False) to skip it with a warning.") warnings.warn(f"{msg}. Skipped.", UserWarning, stacklevel=4) +def _skip_currency(a: ActionRecord, col: str, skipped: list[ActionRecord]) -> None: + """Record and warn that a replayed parse_currency step left the column unchanged.""" + skipped.append(a) + warnings.warn( + f"parse_currency skipped on column {col!r}: it has values that are not currency strings. " + "The column is unchanged.", + UserWarning, + stacklevel=4, + ) + def _bounds(a: ActionRecord) -> tuple[float, float] | None: lower, upper = a.parameters.get("lower"), a.parameters.get("upper") if isinstance(lower, Real) and isinstance(upper, Real): @@ -86,6 +96,7 @@ def suggest_plan(self, df: pd.DataFrame, cfg: CleanConfig) -> CleanPlan: def apply_plan(self, df: pd.DataFrame, plan: CleanPlan, cfg: CleanConfig) -> pd.DataFrame: out = df.copy() + self.skipped: list[ActionRecord] = [] # Execute explicitly planned actions renames = {} for a in plan.actions: @@ -120,6 +131,7 @@ def apply_plan(self, df: pd.DataFrame, plan: CleanPlan, cfg: CleanConfig) -> pd. & ~text.str.fullmatch(CURRENCY_REGEX) ) if invalid_mask.any(): + _skip_currency(a, col, self.skipped) continue cleaned = s.astype(str).str.replace( r"[^\d.-]", "", regex=True @@ -279,6 +291,7 @@ def add(action: ActionRecord, count: pl.Expr | None = None, when: pl.Expr | None def apply_plan(self, df: pl.DataFrame | pl.LazyFrame, plan: CleanPlan, cfg: CleanConfig) -> pl.DataFrame | pl.LazyFrame: import polars as pl + self.skipped: list[ActionRecord] = [] cur = df # 1. Renames renames = {a.column: a.parameters["to"] for a in plan.actions if a.stage == "column_names" and a.action == "normalize_name"} @@ -319,6 +332,7 @@ def apply_plan(self, df: pl.DataFrame | pl.LazyFrame, plan: CleanPlan, cfg: Clea else: has_invalid = validation.item() if has_invalid: + _skip_currency(a, col_name, self.skipped) continue exprs[col_name] = ( expr.cast(pl.String) diff --git a/src/freshdata/_core/engine.py b/src/freshdata/_core/engine.py index b91a76e..472adfe 100644 --- a/src/freshdata/_core/engine.py +++ b/src/freshdata/_core/engine.py @@ -36,13 +36,15 @@ def apply_plan( return_report: bool = False, ) -> Any: cfg = config or CleanConfig() - cleaned: Any = resolve_backend(df).apply_plan(df, plan, cfg) + backend = resolve_backend(df) + cleaned: Any = backend.apply_plan(df, plan, cfg) if return_report: orig_r = 0 if type(df).__name__ == "LazyFrame" else len(df) clean_r = 0 if type(cleaned).__name__ == "LazyFrame" else len(cleaned) orig_c = _column_count(df) clean_c = _column_count(cleaned) - rep = CleanReport(orig_r, clean_r, orig_c, clean_c, plan.actions) + applied = tuple(a for a in plan.actions if not any(a is s for s in backend.skipped)) + rep = CleanReport(orig_r, clean_r, orig_c, clean_c, applied) return cleaned, rep return cleaned diff --git a/tests/test_skipped_currency_report.py b/tests/test_skipped_currency_report.py new file mode 100644 index 0000000..4d4e353 --- /dev/null +++ b/tests/test_skipped_currency_report.py @@ -0,0 +1,25 @@ +"""A replayed parse_currency step that is skipped must not show in the report.""" + +import pandas as pd +import pytest + +import freshdata as fd + +pl = pytest.importorskip("polars") + +PLAN = fd.suggest_plan(pd.DataFrame({"amount": ["$10", "$20", "$30"]})) +NEW = {"amount": ["$50", "REF-9921", "N/A"]} + + +@pytest.mark.parametrize("make", [pd.DataFrame, pl.DataFrame, pl.LazyFrame]) +def test_skipped_parse_currency_warns_and_is_not_reported(make): + assert "parse_currency" in [a.action for a in PLAN.actions] + with pytest.warns(UserWarning, match="parse_currency.*'amount'"): + _, report = fd.apply_plan(make(NEW), PLAN, return_report=True) + assert "parse_currency" not in [a.action for a in report.actions] + + +@pytest.mark.parametrize("make", [pd.DataFrame, pl.DataFrame, pl.LazyFrame]) +def test_applied_parse_currency_is_still_reported(make): + _, report = fd.apply_plan(make({"amount": ["$50", "$60"]}), PLAN, return_report=True) + assert "parse_currency" in [a.action for a in report.actions]