Skip to content

RandomSampleImputer rejects scalar column name 0 for observation seeds #1104

Open

Description

@ktz03

Describe the bug

RandomSampleImputer(seed="observation", random_state=0) rejects column name 0 during construction. The equivalent random_state=[0] works. Integer column names are supported, and 0 is the first column label of a pandas DataFrame created without explicit column names.

To Reproduce

import pandas as pd
from feature_engine.imputation import RandomSampleImputer

X = pd.DataFrame([[1, None], [2, 1.0], [3, 2.0], [4, None]])

# Works: column 0 supplies each observation's seed.
print(RandomSampleImputer(
    variables=[1], seed="observation", random_state=[0]
).fit_transform(X)[1].tolist())
# [2.0, 1.0, 2.0, 1.0]

# Raises ValueError before fit(), despite naming the same column.
RandomSampleImputer(variables=[1], seed="observation", random_state=0)

The constructor tests the truthiness of random_state, so it treats the integer column name 0 as missing.

Expected behavior

Both forms should select column 0 and produce the same imputation. Absent seeding variables (None or an empty list/string) should retain their existing validation.

Screenshots

Not applicable.

Desktop

  • OS: Windows
  • Browser: Not applicable
  • Python 3.12.14, pandas 2.3.3, NumPy 2.2.6, scikit-learn 1.7.2
  • Feature-engine main: d866312874a66364c6dee3c247e69476b478395c

Additional context

The existing Narwhals migration (#965) still has the same constructor guard. This report concerns the scalar integer column label on current main; it does not change the observation seed algorithm or backend support.

I am working on a small constructor validation fix with a regression comparing random_state=0 and [0]. The observation seed algorithm and backend migration remain unchanged.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions