RandomSampleImputer raises TypeError with duplicate indexes when seed="observation"

Đang mở Phù hợp với người mới
#1,141 1 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

Chưa có ai nhận issue này.

Đá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
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Lĩnh vực
data, machine-learning

Hướng nghiên cứu

Bắt đầu với _define_seed() và phép gán trong feature_engine/imputation/random_sample.py, sau đó kiểm tra các bài kiểm thử cho RandomSampleImputer. Tái hiện lỗi bằng các nhãn chỉ mục trùng lặp và seed="observation"; kiểm tra rằng giá trị bị thiếu sử dụng seed của hàng đó, các hàng có chỉ mục trùng lặp vẫn được phân biệt và các giá trị hiện có cùng chỉ mục được giữ nguyên.

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

Mô tả

Describe the bug

RandomSampleImputer raises a TypeError when transforming a DataFrame containing duplicate index labels with seed="observation".

The same data works correctly when the index labels are unique.

Steps to reproduce

import numpy as np
import pandas as pd
from feature_engine.imputation import RandomSampleImputer

train = pd.DataFrame({
    "income": [100.0, 200.0, 300.0, 400.0],
    "seed_col": [1, 2, 3, 4]
})

test = pd.DataFrame({
    "income": [np.nan, 999.0, 300.0],
    "seed_col": [1, 2, 3]
}, index=[0, 0, 1])

imputer = RandomSampleImputer(
    variables=["income"],
    random_state=["seed_col"],
    seed="observation"
)

imputer.fit(train)
imputer.transform(test)

Expected behavior

The missing value should be imputed using the corresponding observation's seed, while preserving the original index and existing non-missing values.

Actual behavior

TypeError: int() argument must be a string,
a bytes-like object or a real number, not 'Series'

Suspected root cause

In feature_engine/imputation/random_sample.py, _define_seed() uses:

internal_seed = int(np.round(
    X.loc[index, seed_variables].sum(), 0
))

When the index label is duplicated, .loc selects multiple observations, resulting in a Series instead of a scalar seed.

Additionally, the assignment X.loc[i, feature] = random_sample could potentially update multiple observations sharing the same index label.

Using positional row access may resolve both concerns.

Environment

  • OS: macOS
  • Python: 3.14.6
  • Feature-engine: 1.9.4
  • Pandas: 3.1.2
  • Scikit-learn: 1.9.1
  • Branch: main
  • Commit: d866312

Additional information

I independently verified that the same example succeeds with unique index labels.

I'd be happy to work on a fix and add regression tests if maintainers agree with the proposed approach.

Thanks!

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.