BUG: Raise ValueError when var_name collides with id_vars in melt()#66036
Closed
C1-BA-B1-F3 wants to merge 1 commit into
Closed
BUG: Raise ValueError when var_name collides with id_vars in melt()#66036C1-BA-B1-F3 wants to merge 1 commit into
C1-BA-B1-F3 wants to merge 1 commit into
Conversation
Fixes GH#65654 - DataFrame.melt silently corrupts data when var_name matches an id_vars column. The fix adds validation to raise a ValueError before the data is overwritten, consistent with the existing value_name collision check. Tests added for both scalar and list-like id_vars cases.
Member
|
closed by #65655 |
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.
Description
Fixes #65654
DataFrame.meltsilently corrupts data whenvar_namematches anid_varscolumn. The originalid_varsdata gets overwritten with the variable labels.Before (silent corruption):
After (ValueError raised):
Changes
pandas/core/reshape/melt.py: Added validation check aftervar_nameis resolved to detect collisions withid_vars. RaisesValueErrorwith a clear message explaining the issue.pandas/tests/reshape/test_melt.py: Added two test cases:test_raise_of_var_name_in_id_vars: Tests scalarid_varscollisiontest_raise_of_var_name_in_id_vars_list: Tests list-likeid_varscollisionTesting
This fix is consistent with the existing
value_namecollision check (line 179-183 in the original code).