Skip to content
Configuration at a glance

Cleaning data

`DataCleaner` applies a small, opinionated set of cleaning rules and records every decision it makes. This page covers the available options and how each column kind is treated.

OptionDefaultEffect
numeric_imputation"median"Numeric fill strategy: median, mean, or constant
categorical_imputation"mode"Categorical fill strategy: mode or constant
numeric_fill_value0.0Fill value when numeric imputation is constant
preserve_integer_dtypesTrueKeep integral numeric columns as int64 when imputation and bounds are integral
on_precision_loss"coerce"Policy when float64 cannot represent a large integer: "raise", "warn", or "coerce"
categorical_fill_value"Unknown"Fill value for constant categorical imputation and unseen labels
cap_outliersFalseClip numeric values to learned IQR bounds (opt-in)
outlier_multiplier1.5IQR multiplier used for the bounds
outlier_columns()Numeric columns to cap; when set, overrides cap_outliers
add_missing_indicatorsFalseAdd <column>__is_missing for columns missing during fit
drop_constant_columnsTrueDrop columns that are constant after cleaning
numeric_columns()Force columns to be treated as numeric
categorical_columns()Force columns to be treated as categorical
datetime_columns()Columns to parse as datetimes
datetime_features("year", "month", "day", "dayofweek", "is_weekend")Calendar features to generate
unseen_category"keep"Keep unseen categories or replace them with the fill value
contractNoneOptional DataContract with column roles and checks
on_drift"warn"Drift policy: raise, warn, or ignore
infer_contractTrueInfer a contract from the training schema
fingerprint_on_fitFalseCompute the training fingerprint during fit
verboseTruePrint a short fit summary
Python
from data_cleaner import CleanerConfig

config = CleanerConfig(
    numeric_imputation="median",
    categorical_imputation="mode",
    cap_outliers=True,
    add_missing_indicators=True,
    drop_constant_columns=True,
    unseen_category="keep",
)

Roles tell the cleaner how to treat a column. They come from a contract, from explicit configuration, or from dtype inference.

RoleTreatment
numericCoerce to numeric, impute, optionally cap outliers
categoryTreat as categorical, impute, apply the unseen policy
booleanCoerce to boolean and impute
datetimeParse and expand into calendar features
idTreat as text; never numeric-impute or cap
textTreat as categorical text; protected from the unseen policy
ignoreDrop from the output

Aliases such as measure, categorical, bool, date, and identifier are normalized to these roles. Unsupported roles raise a ValueError.

Numeric columns are coerced with pd.to_numeric(errors="coerce"), then filled with the strategy learned during fit.

Python
config = CleanerConfig(
    numeric_imputation="median",  # or "mean" / "constant"
    numeric_fill_value=0.0,  # only used with "constant"
    cap_outliers=True,  # opt-in
    outlier_multiplier=1.5,
    outlier_columns=(),  # optional per-column allowlist
)

float64 represents integers exactly only up to 2^53. The cleaner does not silently narrow an integer column: with preserve_integer_dtypes=True (the default), a column that was integral at fit time stays int64 at transform time as long as the learned fill value and outlier bounds are also integral. Columns with fractional medians, fractional IQR bounds, or non-integral values in a batch fall back to float64 and log that fallback.

When a conversion would be lossy, on_precision_loss controls the outcome:

ValueBehavior
"coerce"Convert to float64 and continue (default, matching earlier releases)
"warn"Convert to float64 and log a warning
"raise"Raise ValueError before the value is silently rounded
Python
config = CleanerConfig(
    preserve_integer_dtypes=True,
    on_precision_loss="raise",
)

The check is exact, not a blanket > 2^53 test: values such as 2^53 + 2 are representable and are preserved, while 2^53 + 1 is not. The decision log records a numeric_precision entry during fit when the policy allows a lossy conversion. Store identifiers above 2^53 as text (role="id") when exact round-tripping matters; see Safe ingestion.

Outlier capping is opt-in. With cap_outliers=False (the default), numeric values are imputed but never clipped, so legitimate extremes such as fraud signals or high-value transactions survive unchanged.

Enable it for every numeric column:

Python
CleanerConfig(cap_outliers=True)

Or limit it to specific columns:

Python
CleanerConfig(outlier_columns=("amount", "balance"))

When outlier_columns is non-empty it acts as an allowlist and overrides cap_outliers; every listed column must be numeric.

With capping enabled, the cleaner learns lower and upper IQR bounds from the training column:

lower = Q1 − k · IQR upper = Q3 + k · IQR

where k is outlier_multiplier. Values outside the bounds are clipped, not removed. If the IQR is zero, capping is skipped so constant or near-constant columns are not destroyed.

Inspect the learned bounds:

Python
cleaner.outlier_summary()

Categorical columns are filled with the training mode, or a constant.

Python
config = CleanerConfig(
    categorical_imputation="mode",  # or "constant"
    categorical_fill_value="Unknown",
    unseen_category="keep",  # or "fill"
)

At transform time, a category that did not appear during training has two possible treatments:

PolicyBehavior
"keep"Leave the unseen label in the output
"fill"Replace it with categorical_fill_value

"keep" is the default because one-hot encoders with handle_unknown="ignore" can handle an unseen label safely. Identifier and text columns are protected from the unseen policy.

Boolean-like columns are coerced to pandas nullable booleans, imputed with the learned fill value, and returned as bool. Declare a column with ColumnSpec(..., role="boolean") when its dtype is ambiguous.

Datetime columns must be declared explicitly:

Python
config = CleanerConfig(
    datetime_columns=("joined", "updated_at"),
    datetime_features=("year", "month", "day", "dayofweek", "is_weekend"),
)

The original datetime column is replaced by one column per generated feature, named <source>__<feature>:

Output
joined  ->  joined__year
            joined__month
            joined__day
            joined__dayofweek
            joined__is_weekend

Supported features are year, month, day, dayofweek, quarter, dayofyear, weekofyear, is_weekend, hour, and minute.

Parsing uses mixed-format datetime parsing. Unparseable values become missing values and are then represented by the generated feature nulls. A datetime-like column that is not declared raises an error instead of being silently treated as a category.

Set add_missing_indicators=True to preserve the signal that a value was missing before imputation:

Python
config = CleanerConfig(add_missing_indicators=True)

The cleaner adds an indicator only for columns that actually had missing values during fit, named <column>__is_missing. If that name collides with an existing column, a numeric suffix is used (__is_missing_2, and so on).

With drop_constant_columns=True (the default), columns with a single unique value after cleaning are dropped. This includes columns that became constant because every value was missing and was filled with the same value.

Set it to False if a downstream component expects the column to remain.

Identifiers need different treatment from measurements. Use a contract or a configured role:

Python
from data_cleaner import ColumnSpec, DataContract, CleanerConfig, DataCleaner

contract = DataContract(
    columns=[
        ColumnSpec("customer_id", role="id"),
        ColumnSpec("notes", role="text"),
        ColumnSpec("internal_flag", role="ignore"),
    ]
)

cleaner = DataCleaner(CleanerConfig(contract=contract))

Identifier columns are:

  • rendered without a trailing .0 for integer-like values
  • never numeric-coerced, median-imputed, or outlier-capped
  • not subject to the unseen-category replacement policy

Columns with role="ignore" are dropped from the output.

Every learned or configured rule is recorded:

Python
log = cleaner.decision_log()
log[log["action"] == "cap_outliers"]

The columns are column, action, reason, source, before, and after. Typical actions include impute, cap_outliers, expand_datetime, add_missing_indicator, drop_column, and unseen_category_policy.

Python
config = CleanerConfig(
    numeric_columns=("salary", "age"),
    datetime_columns=("signup_date",),
    datetime_features=("year", "month", "dayofweek", "is_weekend"),
    numeric_imputation="median",
    categorical_imputation="mode",
    cap_outliers=True,
    outlier_multiplier=1.5,
    add_missing_indicators=True,
    drop_constant_columns=True,
    unseen_category="keep",
    contract=contract,
    on_drift="warn",
)

Next: make the assumptions explicit with contracts and drift detection.