fix(labels): accept missing labels, keep label identity, reject duplicates alike - #468
Merged
Merged
Conversation
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)
kevincostner17
changed the base branch from
fix/report-json-and-memory
to
main
September 16, 2026 18:47
…cates alike
Four label defects found by fuzzing:
* pandas coerces Index([0, None]) to float64 with NaN, and then cannot match
that label against the frame's own columns, so DataFrame.duplicated()
raised KeyError(Index([nan])) - breaking clean, profile, infer_roles and
explain_clean on frames every other step handles. Duplicate detection now
addresses columns by position when a label is missing.
* infer_roles collected the labels into one Series, which coerces a mixed
numeric/None or int/float label set, so frame[row["column"]] no longer
round-tripped. The column is built with object dtype instead.
* suggest_plan and plan raised TypeError("cannot convert the series to int")
on duplicate column labels with the semantic layer active, where
infer_roles and explain_clean raise a clear ValueError.
* clean_text and lint_text_encoding raised AttributeError on the same input.
The guard moves to _util.require_unique_labels so every entry point shares
one message; api keeps its private alias.
Closes #459
Closes #461
Closes #462
Closes #437
kevincostner17
force-pushed
the
fix/label-robustness
branch
from
September 16, 2026 18:51
a2d4e73 to
4e9725b
Compare
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
Four column-label defects found by fuzzing. Stacked on #466 — review that first; this PR targets its branch and I'll retarget to
mainonce it merges.suggest_plan/planraiseTypeErroron duplicate labelsNonelabels raiseKeyError(Index([nan]))inclean,profile,infer_rolesandexplain_cleaninfer_rolesreturns coerced labels, breaking the documented round-tripclean_text/lint_text_encodingraiseAttributeErroron duplicate labels (thevalidate_fieldsside shipped in fix(fieldcheck): report bytes, container, boolean and duplicate-label inputs instead of raising #445)Root cause
Index([0, None])to float64, so the second label isNaN.DataFrame.duplicated()then compares itssubsetagainst the frame's own columns and cannot matchNaNagainst itself, raisingKeyError(Index([nan])). Three call sites hit it: the duplicates step (detection and both removal paths), the profile summary, and the role-inference duplicate mask. ANonelabel beside a non-numeric label keeps the index object-dtype, which is why earlier sweeps missed it.infer_rolesbuilt its output frame from a list of row dicts, so pandas collected the labels into one Series and coerced them —0became0.0,NonebecameNaN,-2became-2.0.build_contextdoesint(s.isna().sum())ondf[label], which is a DataFrame when the label repeats. The preview API was therefore stricter than the executor:fd.cleanhandled the frame,fd.plan(df, semantic_mode="auto")did not.clean_textandlint_text_encodingcall.str/.dtypeondf[col], which is a DataFrame for a repeated label.Behaviour change
drop_duplicates,duplicate_subsetand the keep-modes all work on such frames. Frames without missing labels take exactly the previous code path.infer_roles(...)["column"]now holds the frame's own labels with object dtype, soframe[row["column"]]round-trips. Rows are still ordered by label text, as before.suggest_plan,plan,clean_textandlint_text_encodingraiseValueError("<func> requires unique column labels; duplicated: [...]"), matchingfd.validate,fd.infer_roles,explain_clean,detect_piiandbuild_baseline. The guard moved to_util.require_unique_labels;api._require_unique_labelsstays as an alias.Default-output changes
{0: ..., None: ...}) now return a result fromclean,profile,infer_rolesandexplain_cleaninstead of raising, and their duplicate rows are detected and removed like any other frame's.infer_roles: thecolumncolumn is object dtype rather than float64 when labels are mixed numeric/Noneor mixed int/float. Values are the frame's real labels. For all-string labels, output is unchanged.suggest_plan/plan/clean_text/lint_text_encodingon duplicate labels: a clearValueErrorinstead ofTypeError/AttributeError. Unique-label frames are unaffected.One correction to #462 as filed
The issue's repro expects
[0, None]back frompd.DataFrame({0: ..., None: ...}), but pandas coerces those labels to[0.0, nan]at construction, before any freshdata code runs —df.columns.dtypeis already float64, anddf[nan]cannot round-trip in pandas either. That part is not recoverable. What this PR fixes is the coercion freshdata itself introduced: when a frame's columns are genuinely object dtype (pd.Index([0, None], dtype=object),[-2, 0.78],["a", 1]), the labels now come back exactly as they went in and round-trip. The repro therefore still exits 1; the regression test covers the recoverable case.Tests
tests/test_label_robustness.py(new):clean,profile,infer_rolesandexplain_cleanall accept a NaN-label frame; duplicate rows are still detected and removed;duplicate_subsetstill narrows the comparison.[0, None],[-2, 0.78]and["a", 1]object-dtype label sets come back unchanged and round-trip throughdf[label].suggest_plan,plan,clean_text,lint_text_encodingandinfer_rolesall raiseValueErroron duplicate labels without modifying the frame, and all still work on unique labels.Verification
ruff checkandmypy src/freshdata: clean.main. The Duplicate column labels raise AttributeError ('DataFrame' has no attribute 'str'/'dtype') #437 repro passes its text sections. The fd.infer_roles returns coerced column labels (0 -> 0.0, None -> NaN), breaking the documented round-trip #462 repro still exits 1 for the reason above.not online and not largelanes: py3.12 6582 passed, 17 skipped; py3.9 6553 passed, 21 skipped.