DatetimeSubtraction reports dropped reference columns and breaks pandas output

未关闭
#1,103 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@RugvedBane 已经在做这个了。

开始于 2026年11月10日。

  • #1120 来自 @RugvedBane —— 未关闭

评估

难度
3/5
预计耗时
半天
新手友好度
22/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python
领域
data

调研方向

Start with transform in feature_engine/datetime/datetime_subtraction.py, which drops the union of variables_ and reference_, and with _add_new_feature_names in feature_engine/base_transformers/mixins.py, which filters only variables. Reproduce with the snippet in the issue, then check that get_feature_names_out() matches the transform output. Done means both return only end_sub_start and set_output(transform="pandas") works. Tests live in tests/test_datetime/test_datetime_subtraction.py. Open PR #1120 already targets this, so review it before starting.

由索引模型根据 Issue 内容生成。

描述

Describe the bug

With drop_original=True, DatetimeSubtraction correctly removes both operands from the transformed dataframe, but get_feature_names_out() still includes reference columns that are not also in variables. Consequently, the supported set_output(transform="pandas") path raises a column-length mismatch instead of returning the elapsed-time features.

To reproduce

Run against current main, commit d866312874a66364c6dee3c247e69476b478395c:

import pandas as pd

from feature_engine.datetime import DatetimeSubtraction

X = pd.DataFrame({"end": ["2026-01-02"], "start": ["2026-01-01"]})
transformer = DatetimeSubtraction(
    variables=["end"], reference=["start"], drop_original=True
)

result = transformer.fit_transform(X)
print("columns:", result.columns.tolist())
print("names:", transformer.get_feature_names_out())
print("values:", result.to_numpy().tolist())

transformer.set_output(transform="pandas").transform(X)

Output:

columns: ['end_sub_start']
names: ['start', 'end_sub_start']
values: [[1.0]]

The final line raises:

ValueError: Length mismatch: Expected axis has 1 elements, new values have 2 elements

Expected behavior

Both the output columns and get_feature_names_out() should be ['end_sub_start']. Enabling pandas output should return the same one-column dataframe containing 1.0.

Environment

macOS, Python 3.13.16, pandas 3.0.6, NumPy 2.5.3, SciPy 1.18.1 and scikit-learn 1.9.1; Feature-engine imported from the unchanged commit above. This investigation and report were prepared autonomously with AI assistance; no independent human review is claimed.

Additional context

DatetimeSubtraction.transform drops the union of variables_ and reference_. The inherited _add_new_feature_names filters only variables_.

All 83 existing tests in tests/test_datetime/test_datetime_subtraction.py pass. A separate 40-case regression matrix gives 12 failures: six feature-name mismatches and six pandas-output exceptions. These cover distinct/partly overlapping single or multiple references and default/custom output names. The 28 controls pass, including drop_original=False, references contained in variables, and automatic selection. The checks also compare values, nondefault indices, unrelated columns and input preservation.

The active narwhals migration was inspected, not executed: its mixin still filters only variables_; the open column-order fix #1068 does not change this dropped-reference behavior. No library code was changed, and no full-suite, Polars or other-platform validation is claimed.

主要语言
Python
星标
2.3k
派生
391
平均合并
11 小时 34 分钟
30 天内合并 PR
11

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

feature-engine/feature_engine 的其他 Issue

查看 feature-engine/feature_engine 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。