Skip to content

fix(semantic): stop assuming a US locale for every currency - #471

Merged
kevincostner17 merged 1 commit into
mainfrom
fix/currency-locale
Sep 16, 2026
Merged

kevincostner17 merged 1 commit into
mainfrom
fix/currency-locale

Conversation

@kevincostner17

Copy link
Copy Markdown
Contributor

Summary

parse_currency deleted every comma and took the dot as the decimal point, whatever currency was present (semantic/experts.py:122):

cleaned = re.sub(r"[A-Za-z$€£¥₹,\s]", "", s)
return float(cleaned)

An explicit EUR/ is the strongest available signal that the opposite convention is in use, and it was ignored.

Measured before this change

Input Returned Correct Error
EUR 1.200,50 1.2005 1200.50 1000x low
EUR 1.000,00 1.0 1000.00 1000x low
€0,50 50.0 0.50 100x high
EUR 12,5 125.0 12.5 10x high
€1.234.567,89 None 1234567.89 failed to parse
$1,200.50 1200.5 1200.50 correct

€0,50 -> 50.0 turns fifty cents into fifty euros. US formats were all correct, which is why a US-shaped suite never saw it.

Reachable from fd.clean. Under semantic_mode="auto" the repair was applied automatically at confidence 0.98, risk low, so the policy gate at semantic/policy.py:127 had no reason to withhold it.

The correct logic already existed and the currency path bypassed it. NumericFormatExpert parses a bare "1.200,50" as 1200.5 via _EURO_GROUPED, so adding a currency marker made the answer worse.

The fix

Locale is resolved from the value's own punctuation, most reliable evidence first:

  1. Both separators present — the right-most is the decimal one, so €1,234.56 and $1.234,56 both read correctly. Structure beats convention.
  2. One separator, repeated — can only be grouping (1.234.567).
  3. One separator, tail not 3 digits — must be decimal (12,5 → 12.5).
  4. One separator, exactly 3 trailing digits — genuinely ambiguous (1.200 is 1200 in Berlin, 1.2 in Boston). Settled by the currency's documented convention, not guessed. parse_currency_parts() exposes when that happened so a caller can route to review.

Grouping is now validated: "$1.2.3" was silently stripped to 123 and "$1,20.50" to 120.5; both are rejected. Indian lakh grouping (₹1,23,456.70) stays supported.

Compatibility

Default-output change: a European-format currency string in a money column now cleans to its correct magnitude. US and Indian formats are unchanged, and a bare number without a currency marker is still left to ordinary dtype repair.

Verification

  • tests/test_currency_locale.py, 27 cases. 6/6 European cases, both malformed-grouping cases and the end-to-end fd.clean case fail on main; the US/Indian cases pass on both, showing the fix does not regress them.
  • Full suite py3.12: 6586 passed, 22 skipped, 0 failed, coverage 93.92%.
  • ruff check and ruff format clean.

parse_currency deleted every comma and took the dot as the decimal point,
whatever currency was present:

    cleaned = re.sub(r"[A-Za-z$€£¥₹,\s]", "", s)
    return float(cleaned)

An explicit EUR or € is the strongest available signal that the opposite
convention is in use, and it was ignored. Measured before this change:

    EUR 1.200,50   -> 1.2005        (1000x low)
    EUR 1.000,00   -> 1.0           (1000x low)
    €0,50          -> 50.0          (100x high: fifty cents became fifty euros)
    EUR 12,5       -> 125.0         (10x high)
    €1.234.567,89  -> None          (failed to parse)

US formats were all correct, so the defect was invisible to a US-shaped test
suite. It was reachable from fd.clean: under semantic_mode="auto" the repair
landed automatically at confidence 0.98 and risk "low", because the policy
gate had no reason to doubt it.

The library already had the correct logic and the currency path bypassed it --
NumericFormatExpert parses a bare "1.200,50" as 1200.5 via _EURO_GROUPED, so
adding a currency marker made the answer worse.

Locale is now resolved from the value's own punctuation, most reliable
evidence first: with both separators present the right-most is the decimal one
(so a euro amount written the US way still reads correctly, and vice versa); a
repeated separator can only be grouping; a single separator whose tail is not
three digits must be decimal. Only a single separator followed by exactly three
digits is genuinely ambiguous -- "1.200" is 1200 in Berlin and 1.2 in Boston --
and that is settled by the currency's documented convention rather than
guessed. parse_currency_parts() exposes whether an ambiguity was resolved that
way, so a caller can route the cell to review instead.

Grouping is now validated too: "$1.2.3" was silently stripped to 123 and
"$1,20.50" to 120.5. Both are rejected, while Indian lakh grouping
("₹1,23,456.70") stays supported.

tests/test_currency_locale.py covers all of it; 6/6 European cases, both
malformed-grouping cases and the end-to-end fd.clean case fail on the previous
implementation, and the US/Indian cases pass on both so the fix is shown not to
regress them.

Default-output change: a European-format currency string in a money column now
cleans to its correct magnitude.
@coderabbitai

coderabbitai Bot commented Sep 16, 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

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 68276d62-197f-4041-9990-72bacb2892cc


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

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown

FreshData benchmark report — performance

  • freshdata: ?
  • python: ?
  • platform: ?
fixture n_rows n_cols p50 s p95 s peak MB repair % false-repair % preserve % trust monotonic export %

Authored-code reduction (Metric 6)

@kevincostner17
kevincostner17 merged commit 162d316 into main Sep 16, 2026
22 checks passed
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.

2 participants