CategoricalImputer fails when fill_value is already a category
Maintainers usually reply within 1 day
@AryanRG is already working on this.
Since Nov 18, 2026.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 18/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- python, scikit-learn
- Domain
- data
Research direction
Start from the reproduction in the issue, which calls CategoricalImputer(fill_value="unknown", variables=["region"]).fit_transform(X) on a pandas categorical column. The failure comes from the add_categories call inside CategoricalImputer, so read the code around that call first. Done means the fill works for both categorical and ordered categorical columns and keeps the dtype, with regression tests added. Open PR #1114 already targets this, so check it before starting.
Written by the indexing model from the issue text.
Description
CategoricalImputer raises when fill_value is already present in a pandas categorical dtype, instead of filling the missing values.
import pandas as pd
from feature_engine.imputation import CategoricalImputer
X = pd.DataFrame({
"region": pd.Categorical(
["east", None, "west"],
categories=["unknown", "west", "east"],
)
})
CategoricalImputer(fill_value="unknown", variables=["region"]).fit_transform(X)
This raises ValueError: new categories must not include old categories: {'unknown'}. I expected ['east', 'unknown', 'west'], with the categorical dtype preserved. It also fails with an ordered categorical; the equivalent object column works.
Reproduced on current main (d866312), Python 3.12, pandas 2.3.3 and scikit-learn 1.7.2. This is related to #1095, but #1096 changes RareLabelEncoder, not this imputer's separate add_categories call.
I'm happy to contribute the fix and regression tests, and I'm planning to work on this.
- Dominant language
- Python
- Stars
- 2.3k
- Forks
- 391
- Avg merge
- 11h 34m
- Merged PRs (30d)
- 11
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from feature-engine/feature_engine
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
feature-engine/feature_engine#1141 · 1 comment ·
Maintainers usually reply within 1 day
-
docs
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
feature-engine/feature_engine#393 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
feature-engine/feature_engine#1140 · 2 comments ·
Maintainers usually reply within 1 day
-
OneHotEncoder loses a dummy name when it matches an encoded input columnPossibly taken @Aadrit555 claimed this 7 days ago. Open
Difficulty 3/5 Half a day Newbie friendliness 25/100
feature-engine/feature_engine#1106 · 1 comment ·
Maintainers usually reply within 1 day
-
DatetimeSubtraction reports dropped reference columns and breaks pandas outputPossibly taken @RugvedBane claimed this 21 days ago. Open
Difficulty 3/5 Half a day Newbie friendliness 22/100
feature-engine/feature_engine#1103 · 5 comments ·
Maintainers usually reply within 1 day
All issues in feature-engine/feature_engine
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 79/100
developmentseed/titiler-covjson#164 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
awslabs/visual-asset-management-system#2164 ·
Maintainers usually reply within 1 day
-
needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
MakazhanAlpamys/Soup#3589 ·
Maintainers usually reply within 1 day
-
channels status reports the wrong Enabled value for Feishu instances and default-enabled WebSocketOpen
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
Maintainers usually reply within 1 day