Skip to content

fix: prevent data loss during currency parsing - #538

Open
alibro005 wants to merge 1 commit into
FreshCode-Org:mainfrom
alibro005:fix/currency-parsing-data-loss
Open

alibro005 wants to merge 1 commit into
FreshCode-Org:mainfrom
alibro005:fix/currency-parsing-data-loss

Conversation

@alibro005

Copy link
Copy Markdown
Contributor

What changed?

  • Added checks to validate values before converting a column to currency values.
  • Prevented identifiers and other non-currency strings from being incorrectly converted.
  • Skipped conversion when a column contains invalid values.
  • Added a regression test to make sure identifiers are preserved when mixed with currency values.

Why?

Previously, the currency parser removed non-numeric characters before checking whether a value was actually a currency. This could cause values like REF-9921 to be converted into -9921.

This change makes sure that columns containing non-currency values are left unchanged instead of being incorrectly converted.

Fixes #533

How was it tested?

  • Added a regression test in tests/test_stages.py.
  • Ran the full test suite using pytest.
  • Ran the fast CI test lane using pytest -m "not online and not large".
  • Ran Ruff and mypy checks.

Any performance impact?

  • None expected. The validation adds only a small amount of processing during currency parsing.

Any compatibility concerns?

  • No public API changes. Existing valid currency columns continue to be parsed as before.

Documentation updated?

  • No documentation changes were needed for this internal bug fix.

@coderabbitai

coderabbitai Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 982e6704-4384-4573-8d28-41caf7a9e0e5
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@JohnnyWilson16 JohnnyWilson16 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the slow review, @alibro005. This sat for 10 days. Thanks for the fix and the regression test.

I merged it with current main (clean, no conflicts) and ran the suite: 116 passed. The planner part works: suggest_plan no longer plans parse_currency for ["$10.00", "$20.00", "$30.00", "REF-9921"].

1. Replay path still loses data (blocking). apply_plan still strips and coerces with no check (src/freshdata/_core/backends.py:114-116 for pandas, :295-297 for Polars). A plan built on a clean batch and applied to a later batch still breaks it, on both engines:

plan = fd.suggest_plan(pd.DataFrame({"amt": ["$10.00", "$20.00", "$30.00", "$40.00"]}))
fd.apply_plan(pd.DataFrame({"amt": ["$50.00", "REF-9921", "N/A"]}), plan)
# amt -> [50.0, -9921.0, nan]

suggest_plan → apply_plan is the main 3.x path, so #533 is only half fixed. Can you apply the same check in apply_plan (skip the action for that column)? If you'd rather keep this PR small, change Fixes #533 to Part of #533 and I'll open a follow-up.

2. CHANGELOG. Mixed columns now stay strings instead of being parsed. That is a user-visible change, so please add a line under [Unreleased] (CONTRIBUTING.md:54).

3. Nit. The nested-ternary map is hard to read, and it makes stage_dtypes about 3x slower on a 1M-row currency column (0.51 s → 1.65 s). A vectorized check does the same job:

text = out[col].astype(str).str.strip()
invalid = out[col].notna() & text.ne("") & ~text.str.fullmatch(CURRENCY_REGEX)

Happy to merge once 1 and 2 are addressed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(stages): Prevent silent data loss during currency parsing in stage_dtypes

2 participants