diff --git a/feature_engine/_base_transformers/mixins.py b/feature_engine/_base_transformers/mixins.py index 9207873be..610137ca2 100644 --- a/feature_engine/_base_transformers/mixins.py +++ b/feature_engine/_base_transformers/mixins.py @@ -179,7 +179,14 @@ def _add_new_feature_names(self, feature_names): if self.drop_original is True and self.variables_ is not None: # Remove names of variables to drop. - feature_names = [f for f in feature_names if f not in self.variables_] + if self.drop_original is True and self.variables_ is not None: + cols_to_drop = list(self.variables_) + ref = getattr(self, "reference_", None) or getattr( + self, "reference", None + ) + if ref is not None: + cols_to_drop += [r for r in ref if r not in cols_to_drop] + feature_names = [f for f in feature_names if f not in cols_to_drop] return feature_names diff --git a/tests/test_creation/test_relative_features.py b/tests/test_creation/test_relative_features.py index dbfa4972c..538ea2639 100644 --- a/tests/test_creation/test_relative_features.py +++ b/tests/test_creation/test_relative_features.py @@ -459,3 +459,22 @@ def test_get_feature_names_out_raises_error_when_wrong_param( with pytest.raises(ValueError): transformer.get_feature_names_out(input_features=_input_features) + + +def test_get_feature_names_out_drop_original_excludes_reference(): + X = pd.DataFrame({"a": [1.0, 2.0], "b": [3.0, 4.0], "c": [5.0, 6.0]}) + t = RelativeFeatures( + variables=["a"], reference=["c"], func=["add"], drop_original=True + ) + t.fit(X) + assert t.get_feature_names_out() == ["b", "a_add_c"] + + +def test_set_output_pandas_drop_original(): + X = pd.DataFrame({"a": [1.0, 2.0], "b": [3.0, 4.0], "c": [5.0, 6.0]}) + t = RelativeFeatures( + variables=["a"], reference=["c"], func=["add"], drop_original=True + ) + t.fit(X) + result = t.set_output(transform="pandas").transform(X) + assert list(result.columns) == ["b", "a_add_c"] diff --git a/tests/test_datetime/test_datetime_subtraction.py b/tests/test_datetime/test_datetime_subtraction.py index 4e854d04e..c507778f9 100644 --- a/tests/test_datetime/test_datetime_subtraction.py +++ b/tests/test_datetime/test_datetime_subtraction.py @@ -377,3 +377,22 @@ def test_common_tests(estimator): check_feature_names_in(estimator) check_drop_original_variables(estimator) check_return_empty(estimator) + + +def test_get_feature_names_out_drop_original_excludes_reference(): + X = pd.DataFrame({"end": ["2026-01-02"], "start": ["2026-01-01"]}) + transformer = DatetimeSubtraction( + variables=["end"], reference=["start"], drop_original=True + ) + transformer.fit(X) + assert transformer.get_feature_names_out() == ["end_sub_start"] + + +def test_set_output_pandas_drop_original(): + X = pd.DataFrame({"end": ["2026-01-02"], "start": ["2026-01-01"]}) + transformer = DatetimeSubtraction( + variables=["end"], reference=["start"], drop_original=True + ) + transformer.fit(X) + result = transformer.set_output(transform="pandas").transform(X) + assert list(result.columns) == ["end_sub_start"]