[Repo Assist] fix(pss): stop _get_strata from mutating the caller's DataFrame - #1789
Draft
github-actions[bot] wants to merge 1 commit into
Draft
Conversation
PropensityScoreStratificationEstimator._get_strata added four internal columns (strata, dbar, d_y, dbar_y) directly to the DataFrame passed in by the caller. Any code that reused the same DataFrame after calling estimate_effect() would see these unexpected columns in its data. Fix: make a shallow copy of data at the start of _get_strata so the internal bookkeeping columns are confined to that local copy. The clipped subset returned by the method retains the columns it needs for the downstream groupby aggregations in estimate_effect(), but the caller's original DataFrame is unchanged. Also adds a regression test that asserts none of the four internal column names appear on the input DataFrame after estimate_effect() returns. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
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.
🤖 This PR was created by Repo Assist, an automated AI assistant.
Problem
PropensityScoreStratificationEstimator._get_stratadirectly mutates the DataFrame passed to it by adding four internal bookkeeping columns:Because
estimate_effect()passes the caller'sdataargument straight into_get_strata, any code that reuses the same DataFrame after callingestimate_effect()finds four unexpected columns polluting its data.Example of the bug:
Fix
Add
data = data.copy()at the start of_get_strataso all mutations are local to the method. The copy is a shallow/view-friendly pandas copy and adds negligible overhead since_get_stratais called at most a handful of times perestimate_effectinvocation.The
clippedsubset returned by the method still carries the newly added columns (required by the aggregation inestimate_effect()), so correctness is unaffected.Test
Added
test_estimate_effect_does_not_mutate_input_dataframe()totests/causal_estimators/test_propensity_score_stratification_estimator.py. The test:estimate_effectwith PSS.strata,dbar,d_y,dbar_y) are present on the original DataFrame.Test Status
black --checkpasses on all modified filesflake8— no new violations introducedpoetry install(causallearn not available in the CI-lite environment); the logic of_get_stratais unchanged — only a.copy()is prepended