Fix DataFrame accessor mutating original on assignment - #1008
Open
mborodii-prog wants to merge 1 commit into
Open
Fix DataFrame accessor mutating original on assignment#1008mborodii-prog wants to merge 1 commit into
mborodii-prog wants to merge 1 commit into
Conversation
Collaborator
|
Queue triage (2026-07-27)
Please keep the branch current and put the decision in GitHub. This is one of the five active review slots. |
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.
Fix: DataFrame accessor no longer mutates the original on assignment
Problem
When using the
.wranglesaccessor and assigning the result to a new variable, the original DataFrame was also mutated. Both variables ended up pointing to the same in-memory object.The same problem affected any wrangle that modifies columns:
Root cause
Every
make_methodclosure indataframe.pyused this pattern:Two things go wrong:
self._df.__init__(result)calls__init__on the existing object, which reinitialises its data in-place without changing its identity (id()). Sodfis mutated.return self._dfhands back the same reference asdf. Any assignment (df_sample = ...) just creates a second name for the same object.Many wrangle functions also assign columns directly (
df[col] = ...), so even without the__init__trick the original would be modified.The pattern appeared in four places:
_wrangles_accessor.make_method,_wrangles.make_method,_read.make_method, and_read.file.Fix
Pass a copy of the DataFrame into
target_funcand return its result directly — no__init__, no returningself._df:Using
.copy()means wrangle functions that assign columns in-place operate on a throwaway copy, so the caller's original DataFrame is never touched. The returned value is always an independent object.Behaviour after fix
Assigning to a new variable — original unchanged:
Reassignment still works as before:
Multiple calls on the same DataFrame — each independent: