[Repo Assist] fix: preserve DataFrame index in one_hot_encode to fix data_subset_refuter bug with categorical columns#1408
Open
github-actions[bot] wants to merge 1 commit intomainfrom
Conversation
…futer with categorical columns When one_hot_encode() reset the index to 0,1,2,..., the encoded common-causes DataFrame stored in DistanceMatchingEstimator lost the original row indices. If the estimator was called with a subset of the original data (as done by DataSubsetRefuter), the pd.concat inside estimate_effect() would silently misalign rows and the subsequent boolean .loc indexing would raise an IndexingError. Fix: preserve the original DataFrame index throughout one_hot_encode by removing the reset_index(drop=True) calls and passing index=data_to_encode.index when constructing df_encoded. Adds two regression tests to tests/utils/test_encoding.py. Closes #1372 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This was referenced Mar 20, 2026
Member
|
This needs some attention. The existing code in encoding.py was written specifically to fix another bug, where I think one-hot encoding required columns to maintain a specific order. #1112 |
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 is an automated pull request from Repo Assist.
Closes #1372
Root Cause
one_hot_encode()indowhy/utils/encoding.pyunconditionally calledreset_index(drop=True)on both the "columns to keep" slice and the freshly-created encoded DataFrame. This reset every DataFrame's index to0, 1, 2, …regardless of the original row indices.DistanceMatchingEstimator.fit()stores the encoded common-causes DataFrame inself._observed_common_causes. WhenDataSubsetRefutersamples a subset of the original data, the subsetteddataretains its original (non-sequential) index (e.g.[5, 10, 15, …]), whileself._observed_common_causesalways has[0, 1, 2, …].In
estimate_effect(), these two DataFrames are joined withpd.concat(..., axis=1), which aligns on index — causing silent NaN-filling. The immediately following boolean.locindexing then raises:Fix
reset_index(drop=True)from the "columns to keep" slice (it already carries the original index).index=data_to_encode.indexwhen constructing the encodedpd.DataFramefrom the numpy array, so both parts of the finalpd.concatshare the original index.This is a one-line effective change — no behavioural difference for callers that already had a sequential
0, 1, 2, …index.Trade-offs
None identified. All callers downstream use
.valuesfor numerical operations, so preserving the index does not affect those code paths.Test Status
test_one_hot_encode_preserves_indexandtest_one_hot_encode_preserves_index_no_categoricaltotests/utils/test_encoding.py.tests/utils/test_encoding.py).tests/causal_estimators/test_estimator_consistency.py).