Copy receiver_data in autoimpute to avoid mutating caller's frame#185
Merged
Copy receiver_data in autoimpute to avoid mutating caller's frame#185
Conversation
autoimpute ended with
receiver_data[var] = median_imputations[var]
which could write through to the caller's original DataFrame depending
on whether intermediate pandas operations returned a copy or a view.
This is a silent side effect: the user's input frame would gain new
columns without any notification.
Take a defensive .copy() of receiver_data at the top of autoimpute so
the caller's frame is always preserved. The imputed columns are
returned exclusively via result.receiver_data.
Tests
- test_autoimpute_does_not_mutate_caller_receiver asserts that
receiver_data passed in remains pristine and the returned frame
carries the imputed column.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
MaxGhenis
commented
Apr 17, 2026
Contributor
Author
MaxGhenis
left a comment
There was a problem hiding this comment.
autoimpute defensive copy verified:
receiver_data = receiver_data.copy()at function entry before any preprocessing.- Test asserts caller's snapshot is untouched (
pd.testing.assert_frame_equal(receiver, snapshot),'y' not in receiver.columns) while the returnedresult.receiver_datacarries the imputed column. - Performance: single O(N) copy at entry is negligible vs the multiple intermediate frames autoimpute already allocates per model × quantile during imputation.
CI all green. Mergeable. LGTM.
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
Fixes finding #13.
autoimputeended withreceiver_data[var] = median_imputations[var], which could write through to the caller's original DataFrame depending on whether intermediate pandas operations returned a copy or a view. The caller's input frame could silently gain new columns without notification.Take a defensive
.copy()at the top ofautoimputeso the caller's frame is always preserved; imputed columns are returned exclusively viaresult.receiver_data.Test plan
test_autoimpute_does_not_mutate_caller_receiver: caller'sreceiver_datastays pristine, returnedresult.receiver_datahas the imputed column.