fix(semantic): stop assuming a US locale for every currency - #471
Merged
Merged
Conversation
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.
Contributor
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 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. Comment |
FreshData benchmark report —
|
| fixture | n_rows | n_cols | p50 s | p95 s | peak MB | repair % | false-repair % | preserve % | trust | monotonic | export % |
|---|
Authored-code reduction (Metric 6)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
parse_currencydeleted every comma and took the dot as the decimal point, whatever currency was present (semantic/experts.py:122):An explicit
EUR/€is the strongest available signal that the opposite convention is in use, and it was ignored.Measured before this change
EUR 1.200,501.2005EUR 1.000,001.0€0,5050.0EUR 12,5125.0€1.234.567,89None$1,200.501200.5€0,50 -> 50.0turns fifty cents into fifty euros. US formats were all correct, which is why a US-shaped suite never saw it.Reachable from
fd.clean. Undersemantic_mode="auto"the repair was applied automatically at confidence 0.98, risklow, so the policy gate atsemantic/policy.py:127had no reason to withhold it.The correct logic already existed and the currency path bypassed it.
NumericFormatExpertparses a bare"1.200,50"as1200.5via_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,234.56and$1.234,56both read correctly. Structure beats convention.1.234.567).12,5→ 12.5).1.200is 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 to123and"$1,20.50"to120.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-endfd.cleancase fail onmain; the US/Indian cases pass on both, showing the fix does not regress them.ruff checkandruff formatclean.