Repository navigation
Conversation
CodeAnt PR Risk: Medium Risk
Assessed commit: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #45043 +/- ##
=======================================
Coverage 82.55% 82.56%
=======================================
Files 3005 3005
Lines 186589 186616 +27
Branches 43235 43240 +5
=======================================
+ Hits 154046 154076 +30
+ Misses 29781 29778 -3
Partials 2762 2762
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Good catch, and it is a regression I introduced. Confirmed: df = pd.DataFrame({"a": [1], "b": [2], "c": [3]})
list(df.rename(columns={"a": "b", "b": "d"}).columns)
# ['b', 'd', 'c'], unique
The real problem is that the guard tests whether a target label is present rather than whether the rename introduces a duplicate. Flipping
Pushed that guard: rename an empty slice, One relaxation worth a maintainer's eye: a straight swap, |
✅ Deploy Preview for superset-docs-preview ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
I do not think this one holds. Taking the example as written: df = pd.DataFrame([[1, 2, 3]], columns=["a", "a", "b"])
list(df.rename(columns={"a": "c", "b": "a"}).columns)
# ['c', 'c', 'a']Duplicate count is 1 before and 1 after, and The premise is that an existing duplicate can be resolved. It cannot. Checked rather than argued. Over every mapping on frames of 1 to 4 columns drawn from 3 labels onto 4 targets, 6840 in all, the guard agrees with guard = new.duplicated().sum() > idx.duplicated().sum()
truth = new.nunique() < idx.nunique()
# 6840 mappings checked, 0 disagreementsIf there is a case where the count does decrease I would like to see it, since that is the one thing that would break this. |
rusackas
left a comment
There was a problem hiding this comment.
LGTM. Went through the CodeAnt back-and-forth and the monotonicity argument holds up -- rename maps by label, so the image of the distinct labels can only shrink, never grow, which is exactly why comparing duplicate counts catches every real collision without over-rejecting. Good tests.
SUMMARY
rename's collision guard tests whether every new name already exists, so it misses a mapping where only one of them does.{"a": "b", "c": "z"}on a frame holding[a, b, c]slips past it and pandas produces['b', 'b', 'z']. Details in #45042.What makes a mapping invalid is introducing a duplicate label, not a target label happening to be present, so this guards on that instead:
Renaming an empty slice asks pandas the same question the real call will answer, with the same arguments, so the guard cannot drift from the operation it guards. It also keeps the existing level validation, since a bad
levelstill raisesIndexErrororKeyErrorthere.Two notes on why it is shaped this way:
alltoanywas the obvious fix and is not enough. It rejects a mapping that renames onto a label it vacates itself, and it still misses two sources pointing at one new label.renameof an unrelated column should not start failing because of it.BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
On a frame whose columns are
[a, b, c]:{"a": "b"}Label already exists{"a": "x", "c": "z"}['x', 'b', 'z']{"a": "b", "c": "z"}['b', 'b', 'z']Label already exists{"a": "z", "b": "z"}['z', 'z', 'c']Label already exists{"a": "b", "b": "d"}['b', 'd', 'c']{"a": "b", "b": "a"}Label already exists['b', 'a', 'c']Rows three and four are the fix. The last row is the one relaxation: a straight swap produces a unique index and pandas handles it, so it is no longer rejected.
A duplicate label is worth rejecting because
df["b"]then comes back as a DataFrame where callers expect a Series, and the duplicate travels on to the next post-processing operation.TESTING INSTRUCTIONS
Five tests are added:
test_should_raise_exception_partial_duplicationtest_should_raise_exception_partial_duplication_on_multiindexlevel=0test_should_raise_exception_two_sources_one_new_labelanytest_should_rename_onto_a_label_the_same_mapping_vacatesanytest_should_rename_a_frame_that_already_holds_duplicate_labelsThe existing duplication tests both drive the guard with a single-entry mapping, where "every new name exists" and "the result holds a duplicate" are the same condition, which is why this was invisible. The three tests that do pass a two-entry mapping rename onto free names, so they never reach the guard and are unaffected.
The suite goes from 180 to 185 passing with no existing test modified.
ruff check,ruff format --checkandpre-commit run mypyare clean, andpylintrates the changed source file 10.00/10. The error message is unchanged and already extracted, so there is nomessages.potchange.The slice carries no rows, so the guard stays cheap: 0.5 ms on a 1000 column frame and 1.7 ms on a 5000 column one.
ADDITIONAL INFORMATION
renamemapping that partially collides creates a duplicate column label #45042