CategoricalImputer fails when fill_value is already a category

Open
#1,113 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@AryanRG is already working on this.

Since Nov 18, 2026.

  • #1114 by @gopain185 — closed without merging
  • #1122 by @brubasree007 — closed without merging
  • #1125 by @AryanRG — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
18/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from feature-engine/feature_engine

All issues in feature-engine/feature_engine

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.