CategoricalImputer fails when fill_value is already a category

Đang mở
#1,113 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@AryanRG đang làm issue này rồi.

Từ ngày 18/11/2026.

  • #1114 của @gopain185 — đã đóng, không merge
  • #1122 của @brubasree007 — đã đóng, không merge
  • #1125 của @AryanRG — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
18/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python, scikit-learn
Lĩnh vực
data

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
2.3k
Fork
391
Merge trung bình
11 giờ 34 phút
Pull request đã merge (30 ngày)
11

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của feature-engine/feature_engine

Tất cả issue của feature-engine/feature_engine

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.