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

オープン 初心者向け
#1,141 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
pandas, python, scikit-learn

調査の方向性

まず _define_seed() と feature_engine/imputation/random_sample.py の代入を確認し、次に RandomSampleImputer のテストを調べます。重複したインデックスラベルと seed="observation" を使って失敗を再現します。欠損値がその行のシードを使うこと、インデックスが重複する行が別々に保たれること、既存の値とインデックスが保持されることを確認します。

索引モデルが issue の本文から書いたものです。

説明

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!

主要言語
Python
スター
2.3k
フォーク
391
平均マージ
11時間 34分
マージ済み PR(30日)
11

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

feature-engine/feature_engine のほかの issue

feature-engine/feature_engine の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。