OneHotEncoder loses a dummy name when it matches an encoded input column
维护者通常 1 天内回复
@Aadrit555 已经在做这个了。
开始于 2026年11月23日。
评估
- 难度
- 3/5
- 预计耗时
- 半天
- 新手友好度
- 25/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 停滞
- 技术栈
- python
调研方向
The bug is in feature_engine/encoding/one_hot.py: _add_new_feature_names() appends generated names and then filters them against variables_, which drops a generated a_b that matches an input name. Start by reading that method and the transform() method, then reproduce with the snippet in tests/test_encoding/test_onehot_encoder.py style. Done means get_feature_names_out() returns all four indicators (a_b_x, a_b_y, a_b, a_c) and pandas output matches, but PR #1107 is already open against this, so check it first.
由索引模型根据 Issue 内容生成。
描述
Describe the bug
OneHotEncoder mishandles a generated dummy name that matches another categorical input column being encoded. The input column names are unique, and the intended output names are also unique.
On current main, the generated indicator is silently removed, or transformation raises an error if the variables are encoded in the opposite order. On narwhals-migration, transformation preserves the indicator, but get_feature_names_out() still omits its name, so set_output(transform="pandas") raises a length mismatch.
To reproduce
import pandas as pd
from feature_engine.encoding import OneHotEncoder
X = pd.DataFrame(
{"a_b": ["x", "x", "y", "y"], "a": ["b", "c", "b", "c"]}
)
encoder = OneHotEncoder()
result = encoder.fit_transform(X)
print("learned:", encoder.encoder_dict_)
print("columns:", result.columns.tolist())
print("names:", encoder.get_feature_names_out())
print("values:", result.to_numpy().tolist())
encoder.set_output(transform="pandas").transform(X)
On main at d866312874a66364c6dee3c247e69476b478395c:
learned: {'a_b': ['x', 'y'], 'a': ['b', 'c']}
columns: ['a_b_x', 'a_b_y', 'a_c']
names: ['a_b_x', 'a_b_y', 'a_c']
values: [[1, 0, 0], [1, 0, 1], [0, 1, 0], [0, 1, 1]]
The final call also returns only those three columns. With OneHotEncoder(variables=["a", "a_b"]), the same input instead raises:
ValueError: Data must be 1-dimensional, got ndarray of shape (4, 2) instead
On narwhals-migration at 91d01de9d1592a59b1c90c8cf0f74b452513eed7, the same snippet prints:
learned: {'a_b': ['x', 'y'], 'a': ['b', 'c']}
columns: ['a_b_x', 'a_b_y', 'a_b', 'a_c']
names: ['a_b_x', 'a_b_y', 'a_c']
values: [[1, 0, 1, 0], [1, 0, 0, 1], [0, 1, 1, 0], [0, 1, 0, 1]]
Its final call raises:
ValueError: Length mismatch: Expected axis has 4 elements, new values have 3 elements
Separate native Polars checks on the migration branch reproduce the same feature-name omission; its transformed columns and values are correct.
Expected behavior
With the default drop_last=False, all four learned indicators should be returned, and get_feature_names_out() should match:
['a_b_x', 'a_b_y', 'a_b', 'a_c']
There is no final-output naming ambiguity: the original a_b is replaced by a_b_x and a_b_y, leaving a_b available for the indicator of a == "b". Pandas output should return the same four-column result without an exception.
Additional context
The transform restructuring in #1028 is already merged into narwhals-migration and fixes the main-branch transformation problems above. This report also identifies the remaining metadata problem, rather than proposing that restructuring again.
In both versions, OneHotEncoder's own _add_new_feature_names() appends the generated names and then filters the combined list against variables_. This removes the newly generated a_b along with the original input name. On main, transform() similarly concatenates the dummies before dropping the original labels. The open column-order change #1068 does not change this name filtering.
All existing tests in tests/test_encoding/test_onehot_encoder.py pass on both refs: 29 on main and 49 on the migration branch. Separate local regressions reproduce the failures above. Renaming the original a_b column to other is a passing control on both branches, including unseen/missing categories in a later batch and pandas output with a nondefault index. The checks also cover an unrelated numeric column and input preservation.
Environment
macOS arm64; Python 3.13.16, pandas 3.0.6, NumPy 2.5.3, SciPy 1.18.1 and scikit-learn 1.9.1. Migration checks additionally use Narwhals 2.27.0 and Polars 2.0.0. Imports were verified against each unchanged source checkout at the exact commits above.
This investigation and report were prepared autonomously with AI assistance; no independent human review is claimed. No library code was changed, and no full-suite, build or other-platform validation is claimed.
- 主要语言
- Python
- 星标
- 2.3k
- 派生
- 391
- 平均合并
- 11 小时 34 分钟
- 30 天内合并 PR
- 11
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
feature-engine/feature_engine 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
feature-engine/feature_engine#1141 · 1 条评论 ·
维护者通常 1 天内回复
-
docs
难度 2/5 1-3 小时 新手友好度 65/100
feature-engine/feature_engine#393 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 3/5 1-2 天 新手友好度 68/100
feature-engine/feature_engine#1140 · 2 条评论 ·
维护者通常 1 天内回复
-
CategoricalImputer fails when fill_value is already a category可能已有人在做 @AryanRG 于 13 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 18/100
feature-engine/feature_engine#1113 · 2 条评论 ·
维护者通常 1 天内回复
-
DatetimeSubtraction reports dropped reference columns and breaks pandas output可能已有人在做 @RugvedBane 于 21 天前认领。 未关闭
难度 3/5 半天 新手友好度 22/100
feature-engine/feature_engine#1103 · 5 条评论 ·
维护者通常 1 天内回复
查看 feature-engine/feature_engine 的全部 Issue
相似的 Issue
-
[19.0] pos_sale: settling an order with a 0 down payment line crashes (reading 'reward_id') and leaves a wrong amount due可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 79/100
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 79/100
developmentseed/titiler-covjson#164 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 82/100
awslabs/visual-asset-management-system#2164 ·
维护者通常 1 天内回复
-
needs-triage
难度 2/5 1-3 小时 新手友好度 86/100
MakazhanAlpamys/Soup#3589 ·
维护者通常 1 天内回复
-
channels status reports the wrong Enabled value for Feishu instances and default-enabled WebSocket未关闭
难度 2/5 1-3 小时 新手友好度 86/100
维护者通常 1 天内回复