data_preprocessing.imputer

class Imputer(strategy='mean', categorical_features=None, **params)[source]

Bases: DataTransformer

Wrapper around scikit-learn’s SimpleImputer to handle numerical and categorical features separately.

Supports imputation via ‘mean’ (for numerical) and ‘most_frequent’ (for categorical) strategies. Automatically detects and excludes non-declared categorical features (object dtype in numerical slots). Preserves column order and index through fit/transform/inverse_transform.

strategy

Imputation strategy for numerical features (‘mean’ or ‘median’).

Type:

str

categorical_features

List of column names to treat as categorical (matched by exact name).

Type:

List[str]

params

Additional keyword arguments passed to SimpleImputer for numerical imputation.

Type:

dict

numerical_imputer

Imputer instance for numerical features.

Type:

SimpleImputer

categorical_imputer

Imputer instance for categorical features (always ‘most_frequent’).

Type:

SimpleImputer

n_features_in_

Number of features seen during fit.

Type:

int

feature_names_in_

Names of features seen during fit.

Type:

List[str]

feature_names_out_

Names of features output after transform.

Type:

List[str]

input_index_

Index of input DataFrame during fit.

Type:

pd.Index

numerical_columns

Column names identified as numerical.

Type:

List[str]

categorical_columns

Column names identified as categorical.

Type:

List[str]

non_declared_categorical_features

Object-type columns detected in numerical slots and excluded.

Type:

List[str]

Initializes the Imputer with specified strategy and categorical feature handling.

Parameters:
  • strategy (str) – Imputation strategy for numerical features. Supports ‘mean’ (default) and ‘median’. Categorical features are always imputed using ‘most_frequent’.

  • categorical_features (list) – List of column names to treat as categorical. Columns matching any of these (by exact name) are treated as categorical; others as numerical. Non-declared object-type columns in numerical slots are automatically detected and excluded. Defaults to None (interpreted as empty list).

  • **params – Additional keyword arguments passed to SimpleImputer for numerical imputation. Example: {‘add_indicator’: True}.

Raises:

ValueError – If strategy is not supported (‘mean’ or ‘median’).

fit(x, y=None)[source]

Fits the imputer separately for numerical and categorical features.

Identifies and separates features by type, logs warnings for non-declared categorical columns, and fits SimpleImputer instances on their respective subsets.

Parameters:
  • x (DataFrame) – Input feature DataFrame.

  • y (optional) – Target variable. Ignored; included for scikit-learn API compatibility.

Returns:

The fitted imputer instance (self), for method chaining.

Return type:

Imputer

Raises:
  • TypeError – If x is not a pandas DataFrame.

  • ValueError – If numerical columns cannot be safely converted (via downstream usage).

get_feature_names_out(input_features=None)[source]

Returns ordered list of output feature names.

The output feature names are the concatenation of numerical_columns and categorical_columns, in the order they were identified during fit.

Parameters:

input_features – Ignored; included for scikit-learn API compatibility.

Returns:

Ordered list of output feature names.

Return type:

List[str]

Raises:

ValueError – If imputer has not been fitted (via check_is_fitted).

inverse_transform(x)[source]

Returns input DataFrame with columns reordered to match original feature order.

This method preserves the index of the input x and does not reverse imputation (since original NaN positions are not stored). Included for scikit-learn compatibility.

Parameters:

x (DataFrame) – Transformed DataFrame (output of transform()).

Returns:

DataFrame with columns reordered to match feature_names_in_. The index of x is preserved.

Return type:

DataFrame

Raises:

ValueError – If imputer has not been fitted (via check_is_fitted).

transform(x)[source]

Applies imputation to input DataFrame, handling numerical and categorical columns separately.

Steps: 1. Validates presence of all training features. 2. Harmonizes numerical column dtypes to float. 3. Applies numerical_imputer and categorical_imputer independently. 4. Reconcatenates and reorders columns to match original feature order.

Parameters:

x (DataFrame) – Input feature DataFrame, with same columns as used in fit().

Returns:

Imputed DataFrame with same column names and original index.

Return type:

DataFrame

Raises:
  • ValueError – If input is missing columns seen during fit.

  • ValueError – If a numerical column cannot be converted to float.

  • ValueError – If imputer has not been fitted (via check_is_fitted).