From f2e500da6b6554bd03fe7e0237e443a3cb1b4141 Mon Sep 17 00:00:00 2001
From: ChrisW09 <50968720+ChrisW09@users.noreply.github.com>
Date: Fri, 16 Oct 2026 14:20:59 +0200
Subject: [PATCH] fix(preprocessor): keep a block for columns with no observed
value at fit
A column missing on every fit row (e.g. an optional field that is empty in
a training window or a cross-validation fold) was dropped by the
SimpleImputer, scikit-learn's default for such a column. The rest of its
per-column pipeline then got no input: numerical methods and one-hot
failed with "Found array with 0 feature(s)", while "int" and "none"
silently removed the column from the output, output_dims_, the blocks and
the lineage, so get_feature_info() and verbose=2 fits raised an
IndexError.
The imputers now keep such a column (keep_empty_features=True) and fill
it with 0, or fill_value for strategy="constant", so every feature keeps
its block and later data where it is observed transforms with the fitted
width. fit emits a DataWarning naming the column, and the constant policy
treats an empty numerical column as constant. A method that cannot be
fitted on such a column (the splines and Box-Cox, or with imputation
disabled every method except the scalers, "quantile" and "none") raises a
PretabDataError naming it, chained to the method's own error. The column
is only blamed when its block fits a stand-in column of observed values
with the same y, so an unrelated failure (e.g. a y of the wrong length)
keeps its own error. get_feature_info no longer raises on a 0-width "int"
block.
Fixes #95
---
docs/core_concepts/missing_values.md | 9 +
docs/representations/choosing_a_method.md | 9 +
pretab/compose/factory.py | 22 ++-
pretab/compose/inspection.py | 7 +-
pretab/preprocessor.py | 91 +++++++++-
tests/compose/test_factory.py | 10 ++
tests/compose/test_inspection.py | 20 ++-
tests/integration/test_edge_case_contract.py | 177 ++++++++++++++++++-
tests/integration/test_verbosity.py | 12 +-
9 files changed, 341 insertions(+), 16 deletions(-)
diff --git a/docs/core_concepts/missing_values.md b/docs/core_concepts/missing_values.md
index 4729edb..4cda03d 100644
--- a/docs/core_concepts/missing_values.md
+++ b/docs/core_concepts/missing_values.md
@@ -36,6 +36,15 @@ unchanged at `transform`. PreTab never drops a row for missing values: every inp
produces an output row.
```
+```{note}
+A column with no observed value at `fit` (for example an optional field that is empty in a
+training window or a cross-validation fold) still keeps its block, so the output layout does not
+depend on which rows were seen. The imputers fill it with `0` (or `fill_value` for
+`strategy="constant"`), which makes it a constant column, and `fit` emits a `DataWarning`
+naming it. See [Edge-case behaviour](../representations/choosing_a_method.md) for the methods
+that cannot be fitted on a constant column.
+```
+
## The `missing_policy` control
For finer control, `missing_policy` selects one of five behaviours for the whole
diff --git a/docs/representations/choosing_a_method.md b/docs/representations/choosing_a_method.md
index 2e457d5..654cb27 100644
--- a/docs/representations/choosing_a_method.md
+++ b/docs/representations/choosing_a_method.md
@@ -98,6 +98,15 @@ PreTab is explicit about degenerate inputs rather than failing silently.
row for it.
- **NaN into a finite-only method**: raises a typed error unless imputation is configured. See
[Missing values](../core_concepts/missing_values.md).
+- **Column with no observed value at fit** (missing on every row, e.g. an optional field that is
+ empty in a training window or a cross-validation fold): `Preprocessor` keeps the column's
+ block and emits a `DataWarning` naming it. The imputers fill it with `0` (or `fill_value`
+ for `strategy="constant"`), so it is fitted as a constant column; a method that cannot be
+ fitted on a constant column (the splines above, or Box-Cox) raises a typed error naming the
+ column. With imputation disabled the column stays missing: the scalers, `"quantile"` and
+ `"none"` keep its block as missing values, and every other method raises the same typed
+ error. `policy={"constant": "error"}` rejects an empty numerical column like any other
+ constant one.
## Non-goals
diff --git a/pretab/compose/factory.py b/pretab/compose/factory.py
index 8ced4d6..d28f182 100644
--- a/pretab/compose/factory.py
+++ b/pretab/compose/factory.py
@@ -54,6 +54,18 @@ def _filter_kwargs(allowed, kwargs):
return {key: kwargs[key] for key in allowed if key in kwargs}
+def _imputer(strategy, add_indicator, imputer_kwargs):
+ """Build the leading :class:`~sklearn.impute.SimpleImputer` of a per-column pipeline.
+
+ A column with no observed value at fit is kept (``keep_empty_features=True``)
+ and filled with ``0``, or with ``fill_value`` for ``strategy="constant"``.
+ scikit-learn's default drops it, which leaves the rest of the pipeline an
+ input without columns: the block then fails to fit or vanishes from the output.
+ """
+ imputer_kwargs = {"keep_empty_features": True, **(imputer_kwargs or {})}
+ return SimpleImputer(strategy=strategy, add_indicator=add_indicator, **imputer_kwargs)
+
+
def _clamp_spline_basis(output_dim):
"""Clamp a requested output dimension into the supported B/M/I spline range.
@@ -116,10 +128,7 @@ def get_numerical_transformer_steps(
steps = []
if add_imputer:
- imputer_kwargs = imputer_kwargs or {}
- steps.append(
- ("imputer", SimpleImputer(strategy=imputer_strategy, add_indicator=add_missing_indicator, **imputer_kwargs))
- )
+ steps.append(("imputer", _imputer(imputer_strategy, add_missing_indicator, imputer_kwargs)))
# Optional scaling step, added only when it is not already the chosen method.
scalers = {
@@ -193,10 +202,7 @@ def get_categorical_transformer_steps(
steps = []
if add_imputer:
- imputer_kwargs = imputer_kwargs or {}
- steps.append(
- ("imputer", SimpleImputer(strategy=imputer_strategy, add_indicator=add_missing_indicator, **imputer_kwargs))
- )
+ steps.append(("imputer", _imputer(imputer_strategy, add_missing_indicator, imputer_kwargs)))
if method not in CATEGORICAL_METHODS:
raise invalid_param_error(
diff --git a/pretab/compose/inspection.py b/pretab/compose/inspection.py
index 7f3b9b3..ffd71cf 100644
--- a/pretab/compose/inspection.py
+++ b/pretab/compose/inspection.py
@@ -160,8 +160,11 @@ def build_feature_info(column_transformer, *, embeddings, embedding_dimensions):
elif "continuous_ordinal" in steps:
step = representation_pipeline.named_steps["continuous_ordinal"]
- categories = len(step.mapping_[columns.index(feature_name)])
- dimension = separate_state_dimension if separate_state_dimension is not None else 1
+ position = columns.index(feature_name)
+ # An encoder that received no column (a 0-width block) has no mapping.
+ has_mapping = position < len(step.mapping_)
+ categories = len(step.mapping_[position]) if has_mapping else None
+ dimension = separate_state_dimension if separate_state_dimension is not None else int(has_mapping)
categorical_feature_info[feature_name] = {
"preprocessing": preprocessing_type,
"dimension": dimension,
diff --git a/pretab/preprocessor.py b/pretab/preprocessor.py
index a8193b8..4b65553 100644
--- a/pretab/preprocessor.py
+++ b/pretab/preprocessor.py
@@ -3,7 +3,7 @@
import os
import time
import warnings
-from typing import cast
+from typing import Any, cast
import numpy as np
from scipy import sparse as sp
@@ -34,8 +34,10 @@
from .core.policy import RepresentationPolicy, apply_constant_policy
from .exceptions import (
ConfigWarning,
+ DataWarning,
FrozenRepresentationError,
OutputBudgetError,
+ PretabConfigError,
PretabDataError,
PretabSerializationError,
invalid_param_error,
@@ -341,6 +343,11 @@ class Preprocessor(TransformerMixin, BaseEstimator):
``"dummy"`` -> ``"one-hot"``, ``"ordinal"`` / ``"label"`` -> ``"int"``, ``"poly"`` ->
``"polynomial"``, and ``"passthrough"`` -> ``"none"``.
+ A column with no observed value at ``fit`` keeps its block: the imputers fill it with ``0``
+ (or ``fill_value`` for ``strategy="constant"``), so it is fitted as a constant column, and
+ ``fit`` emits a :class:`~pretab.exceptions.DataWarning` naming it. A method that cannot be
+ fitted on such a column raises a :class:`~pretab.exceptions.PretabDataError` naming it.
+
``transform`` returns a single stacked array by default (``output_structure="matrix"``),
or a dict of per-feature blocks keyed ``num_
`` / ``cat_`` when
``output_structure="blocks"``. Passing ``return_array`` explicitly to ``transform`` /
@@ -478,6 +485,19 @@ def fit(self, X, y=None, embeddings=None):
-------
self : Preprocessor
Fitted instance of the preprocessor.
+
+ Raises
+ ------
+ PretabDataError
+ If a column has no observed (non-missing) value and its method cannot be
+ fitted on such a column (e.g. the splines or Box-Cox on the imputed constant).
+ The message names the column.
+
+ Warns
+ -----
+ DataWarning
+ If a column has no observed (non-missing) value. Its block is kept and fitted
+ on the entirely missing column, a single constant once imputed.
"""
if self.is_frozen():
@@ -546,8 +566,24 @@ def fit(self, X, y=None, embeddings=None):
self.policy_ = RepresentationPolicy.resolve(self.policy)
self.numerical_features_ = list(numerical_features)
self.categorical_features_ = list(categorical_features)
+ # A feature without any observed value (e.g. an optional field that is empty
+ # in this training window or CV fold) still gets its block, fitted on a
+ # column that is missing throughout (a single constant once imputed).
+ empty_features = list(X.columns[X.isna().to_numpy().all(axis=0)]) if len(X) else []
+ if empty_features:
+ warnings.warn(
+ f"{type(self).__name__} received column(s) {empty_features} with no observed (non-missing) "
+ "value at fit. Their blocks are kept but fitted on an entirely missing column (a single "
+ "constant value once imputed), so they carry no information until the preprocessor is "
+ "refit on data where those columns are observed.",
+ DataWarning,
+ stacklevel=2,
+ )
if numerical_features and self.policy_.constant != "allow":
- numeric_values = X[numerical_features].to_numpy(dtype=np.float64, na_value=np.nan)
+ numeric_values = X[numerical_features].to_numpy(dtype=np.float64, na_value=np.nan, copy=True)
+ # An empty column is represented by one constant value, so the policy
+ # treats it like any other constant column.
+ numeric_values[:, np.isnan(numeric_values).all(axis=0)] = 0.0
apply_constant_policy(numeric_values, self.policy_, estimator=self)
valid_formats = ("auto", "dense", "sparse")
@@ -580,7 +616,12 @@ def fit(self, X, y=None, embeddings=None):
categorical_features,
sparse_threshold=sparse_threshold,
)
- self.column_transformer_.fit(X, y)
+ try:
+ self.column_transformer_.fit(X, y)
+ except ValueError as exc:
+ if empty_features and not isinstance(exc, PretabConfigError):
+ self._raise_for_empty_features(X, y, empty_features, config)
+ raise
self.n_features_in_ = X.shape[1]
self._enforce_output_budget(X.shape[0])
@@ -881,6 +922,50 @@ def _reject_missing(self, X) -> None:
"('propagate', 'impute', 'impute_with_indicator', 'separate_state')."
)
+ def _raise_for_empty_features(self, X, y, empty_features, config) -> None:
+ """Name the empty feature behind a failed ``fit``, if one is to blame.
+
+ Called when fitting the ColumnTransformer failed. The block of each feature
+ without an observed value is refitted on its own, and the first that fails
+ raises a :class:`~pretab.exceptions.PretabDataError` naming that feature,
+ chained to the block's own error: methods that cannot be fitted on a
+ constant column (e.g. the B/M/I and cubic splines, Box-Cox) otherwise only
+ report "the feature at index 0". A block is only blamed when it fits a
+ stand-in column of distinct observed values with the same ``y``, so a
+ failure with another cause (e.g. a ``y`` of the wrong length) is not
+ attributed to the empty column. Returns without raising when no empty
+ column is to blame, so the caller re-raises the original error.
+ """
+ empty = set(empty_features)
+ for _name, transformer, columns in self.column_transformer_.transformers:
+ feature = columns[0]
+ if feature not in empty:
+ continue
+ is_numerical = feature in self.numerical_features_
+ try:
+ cast(Any, clone(transformer)).fit(X[columns], y)
+ except PretabConfigError:
+ continue # a configuration problem, not one of the empty column
+ except ValueError as exc:
+ stand_in = X[columns].copy()
+ stand_in[columns[0]] = (
+ np.linspace(1.0, 2.0, len(X))
+ if is_numerical
+ else np.resize(np.array(["a", "b"], dtype=object), len(X))
+ )
+ try:
+ cast(Any, clone(transformer)).fit(stand_in, y)
+ except ValueError:
+ continue # the block fails with observed values too: not the emptiness
+ method = config.method_for(feature, is_numerical=is_numerical)
+ raise PretabDataError(
+ f"Column {feature!r} has no observed (non-missing) value at fit, and its {method!r} "
+ "representation cannot be fitted on such a column (entirely missing, or a single "
+ f"constant once imputed): {exc}\n"
+ "Fix: drop the column, fit on data where it is observed, or select a method that "
+ "accepts a constant column for it via feature_preprocessing."
+ ) from exc
+
def _enforce_output_budget(self, n_rows: int) -> None:
"""Check the fitted output width against the configured output budget.
diff --git a/tests/compose/test_factory.py b/tests/compose/test_factory.py
index c7b7488..466aeea 100644
--- a/tests/compose/test_factory.py
+++ b/tests/compose/test_factory.py
@@ -3,6 +3,7 @@
import numpy as np
import pytest
from sklearn.compose import ColumnTransformer
+from sklearn.pipeline import Pipeline
from pretab.compose.factory import (
_placement_kwargs,
@@ -29,6 +30,15 @@ def test_imputer_omitted_when_disabled():
assert "imputer" not in _names(get_numerical_transformer_steps("standardization", add_imputer=False))
+def test_imputer_keeps_a_column_without_observed_values():
+ empty = np.full((4, 1), np.nan)
+ assert Pipeline(get_numerical_transformer_steps("minmax")).fit_transform(empty).shape == (4, 1)
+ assert Pipeline(get_categorical_transformer_steps("one-hot")).fit_transform(empty.astype(object)).shape == (4, 1)
+ # imputer_kwargs can still restore scikit-learn's default of dropping it.
+ steps = dict(get_numerical_transformer_steps("minmax", imputer_kwargs={"keep_empty_features": False}))
+ assert steps["imputer"].keep_empty_features is False
+
+
def test_none_method_uses_noop_step():
assert _names(get_numerical_transformer_steps("none", add_imputer=False)) == ["noop"]
diff --git a/tests/compose/test_inspection.py b/tests/compose/test_inspection.py
index 1921e62..c1933f3 100644
--- a/tests/compose/test_inspection.py
+++ b/tests/compose/test_inspection.py
@@ -1,10 +1,14 @@
"""Unit tests for :mod:`pretab.compose.inspection`."""
+import warnings
+
import numpy as np
import pandas as pd
import pytest
+from sklearn.compose import ColumnTransformer
+from sklearn.pipeline import Pipeline
-from pretab.compose.factory import build_column_transformer
+from pretab.compose.factory import build_column_transformer, get_categorical_transformer_steps
from pretab.compose.inspection import (
build_feature_info,
build_transformer_summary,
@@ -56,6 +60,20 @@ def test_build_feature_info_does_not_infer_kind_from_cat_in_feature_name(make_co
assert feature not in categorical
+def test_build_feature_info_reports_a_zero_width_block():
+ # scikit-learn's imputer default drops a column without observed values, so
+ # the encoder after it receives no column at all.
+ steps = get_categorical_transformer_steps("int", imputer_kwargs={"keep_empty_features": False})
+ ct = ColumnTransformer([("cat_city", Pipeline(steps), ["city"])])
+ with warnings.catch_warnings():
+ warnings.filterwarnings("ignore", "Skipping features without any observed values", UserWarning)
+ ct.fit(pd.DataFrame({"city": pd.Series([np.nan] * 4, dtype=object)}))
+
+ _, categorical, _ = build_feature_info(ct, embeddings=False, embedding_dimensions={})
+
+ assert categorical["city"]["dimension"] == 0
+
+
def test_build_transformer_summary_has_header_and_rows():
numerical = {"age": {"preprocessing": "imputer -> standardization", "dimension": 1, "categories": None}}
categorical = {"city": {"preprocessing": "imputer -> continuous_ordinal", "dimension": 1, "categories": 3}}
diff --git a/tests/integration/test_edge_case_contract.py b/tests/integration/test_edge_case_contract.py
index 97e8b07..c9fadc3 100644
--- a/tests/integration/test_edge_case_contract.py
+++ b/tests/integration/test_edge_case_contract.py
@@ -15,7 +15,7 @@
import pandas as pd
import pytest
-from pretab import Preprocessor, RepresentationPolicy
+from pretab import CrossFittedTransformer, Preprocessor, RepresentationPolicy
from pretab.exceptions import DataWarning, InsufficientSamplesError, PretabDataError
from pretab.transformers import (
BSplineTransformer,
@@ -258,3 +258,178 @@ def test_preprocessor_stores_resolved_policy(rng):
pre.fit(df, y)
assert isinstance(pre.policy_, RepresentationPolicy)
assert pre.policy_.constant == "warn"
+
+
+# --------------------------------------------------------------------------- #
+# Column with no observed value at fit, through the Preprocessor
+# --------------------------------------------------------------------------- #
+def _frame_with_empty_column(rng, kind, n=60):
+ """A frame whose ``empty`` column (numerical or categorical) is missing on every row."""
+ empty = np.full(n, np.nan) if kind == "numerical" else pd.Series([np.nan] * n, dtype=object)
+ return pd.DataFrame({"age": rng.normal(40, 10, n), "city": rng.choice(["a", "b", "c"], n), "empty": empty})
+
+
+def _observed(X, kind, rng):
+ """``X`` with its ``empty`` column observed on every row (as later data may be)."""
+ values = rng.normal(size=len(X)) if kind == "numerical" else rng.choice(["p", "q"], len(X))
+ return X.assign(empty=values)
+
+
+def _fit_empty(pre, X, y):
+ with pytest.warns(DataWarning, match=r"\['empty'\] with no observed"):
+ return pre.fit(X, y)
+
+
+def _assert_metadata_matches_output(pre, X, X_later):
+ """Every detected feature has a block, and every metadata view agrees with the output."""
+ out = pre.transform(X, return_array=True)
+ width = out.shape[1]
+ assert pre.n_features_in_ == 3
+ assert set(pre.output_dims_) == {"age", "city", "empty"}
+ assert pre.output_dims_["empty"] >= 1
+ assert sum(pre.output_dims_.values()) == width == pre.total_output_dim_
+ assert len(pre.get_feature_names_out()) == width
+ lineage = pre.get_feature_lineage()
+ assert len(lineage) == width
+ assert sum(record.source_features == ("empty",) for record in lineage) == pre.output_dims_["empty"]
+ blocks = pre.transform(X, return_array=False)
+ assert {name.split("_", 1)[1] for name in blocks} == {"age", "city", "empty"}
+ numerical, categorical, _ = pre.get_feature_info(verbose=False)
+ assert set(numerical) | set(categorical) == {"age", "city", "empty"}
+ info = {**numerical, **categorical}["empty"]
+ # The imputer's own missing indicator (imputation with add_missing_indicator or
+ # "impute_with_indicator") runs through the representation step, and
+ # get_feature_info does not count it for every method; that is a separate
+ # defect, so the dimension is compared only for blocks without it.
+ has_imputer_indicator = any("__missingindicator_" in str(name) for name in pre.get_feature_names_out())
+ if info["dimension"] is not None and not has_imputer_indicator:
+ assert info["dimension"] == pre.output_dims_["empty"]
+ # Data where the column is observed later is transformed with the fitted width.
+ assert pre.transform(X_later, return_array=True).shape == (len(X_later), width)
+
+
+# Numerical methods that fit a constant column, with (target_aware, placement_strategy).
+_EMPTY_NUMERICAL_CASES = [
+ (method, target_aware, "cart" if target_aware else "uniform")
+ for method in [
+ "ple",
+ "minmax",
+ "standardization",
+ "robust",
+ "quantile",
+ "yeo-johnson",
+ "rbf",
+ "relu",
+ "sigmoid",
+ "tanh",
+ "binning",
+ "fourier",
+ "polynomial",
+ "none",
+ ]
+ for target_aware in (True, False)
+ if not (method == "ple" and not target_aware)
+]
+
+
+@pytest.mark.parametrize(("method", "target_aware", "placement"), _EMPTY_NUMERICAL_CASES)
+def test_empty_numerical_column_keeps_its_block(method, target_aware, placement, rng):
+ X = _frame_with_empty_column(rng, "numerical")
+ y = rng.normal(size=len(X))
+ pre = Preprocessor(numerical_method=method, target_aware=target_aware, placement_strategy=placement)
+ _fit_empty(pre, X, y)
+ _assert_metadata_matches_output(pre, X, _observed(X, "numerical", rng))
+
+
+# The methods that cannot be fitted on a constant column (see CONSTANT_RAISES).
+@pytest.mark.parametrize(
+ "method", ["bspline", "mspline", "ispline", "pspline", "naturalspline", "cubicspline", "box-cox"]
+)
+def test_empty_numerical_column_names_the_column_when_the_method_needs_spread(method, rng):
+ X = _frame_with_empty_column(rng, "numerical")
+ y = rng.normal(size=len(X))
+ pre = Preprocessor(numerical_method=method, target_aware=False, placement_strategy="uniform")
+ with pytest.warns(DataWarning), pytest.raises(PretabDataError, match=f"Column 'empty' .* {method!r}") as info:
+ pre.fit(X, y)
+ assert info.value.__cause__ is not None # chained to the method's own error
+
+
+@pytest.mark.parametrize(
+ ("method", "y_kind", "message"),
+ [
+ ("ple", "short", "same length"),
+ ("bspline", "strings", "could not convert string to float"),
+ ("rbf", "strings", "could not convert string to float"),
+ ],
+)
+def test_empty_column_is_not_blamed_for_an_unrelated_fit_failure(method, y_kind, message, rng):
+ """A failure the block also hits with observed values (here a bad ``y``) keeps
+ its own error instead of being attributed to the empty column."""
+ X = _frame_with_empty_column(rng, "numerical")
+ y = rng.normal(size=len(X) - 3) if y_kind == "short" else rng.choice(["p", "q"], len(X))
+ params = {"target_aware": True, "placement_strategy": "cart"} if method != "ple" else {}
+ with pytest.warns(DataWarning), pytest.raises(ValueError, match=message) as info:
+ Preprocessor(numerical_method=method, **params).fit(X, y)
+ assert "has no observed" not in str(info.value)
+
+
+@pytest.mark.parametrize(
+ "params",
+ [
+ {"missing_policy": "impute"},
+ {"missing_policy": "impute_with_indicator"},
+ {"missing_policy": "separate_state"},
+ {"missing_policy": "propagate"},
+ {"add_missing_indicator": True},
+ {"numerical_imputation": None},
+ {"numerical_imputation": None, "add_missing_indicator": True},
+ {"numerical_imputation": "constant"},
+ ],
+)
+def test_empty_numerical_column_under_missing_value_settings(params, rng):
+ X = _frame_with_empty_column(rng, "numerical")
+ y = rng.normal(size=len(X))
+ pre = Preprocessor(numerical_method="minmax", **params)
+ with warnings.catch_warnings():
+ # The all-NaN column reaches MinMaxScaler when imputation is disabled.
+ warnings.filterwarnings("ignore", "All-NaN slice", RuntimeWarning)
+ _fit_empty(pre, X, y)
+ _assert_metadata_matches_output(pre, X, _observed(X, "numerical", rng))
+
+
+@pytest.mark.parametrize("method", ["int", "one-hot", "none"])
+@pytest.mark.parametrize(
+ "params",
+ [
+ {},
+ {"missing_policy": "impute_with_indicator"},
+ {"missing_policy": "separate_state"},
+ {"missing_policy": "propagate"},
+ {"add_missing_indicator": True},
+ {"categorical_imputation": None},
+ {"categorical_imputation": "constant"},
+ ],
+)
+def test_empty_categorical_column_keeps_its_block(method, params, rng):
+ X = _frame_with_empty_column(rng, "categorical")
+ y = rng.normal(size=len(X))
+ pre = Preprocessor(numerical_method="minmax", categorical_method=method, **params)
+ _fit_empty(pre, X, y)
+ _assert_metadata_matches_output(pre, X, _observed(X, "categorical", rng))
+
+
+def test_empty_numerical_column_counts_as_constant_for_the_policy(rng):
+ X = _frame_with_empty_column(rng, "numerical")
+ y = rng.normal(size=len(X))
+ with pytest.warns(DataWarning), pytest.raises(PretabDataError, match="constant"):
+ Preprocessor(numerical_method="minmax", policy={"constant": "error"}).fit(X, y)
+
+
+def test_column_empty_in_a_cross_fitting_fold_keeps_the_width(rng):
+ n = 60
+ X = _frame_with_empty_column(rng, "numerical", n=n)
+ X.loc[n - 6 :, "empty"] = rng.normal(size=6) # observed only in the last fold
+ y = rng.normal(size=n)
+ cross_fitted = CrossFittedTransformer(Preprocessor(numerical_method="rbf"), n_folds=10, shuffle=False)
+ out_of_fold = np.asarray(cross_fitted.fit_transform(X, y))
+ assert out_of_fold.shape == cross_fitted.transform(X).shape
diff --git a/tests/integration/test_verbosity.py b/tests/integration/test_verbosity.py
index 3c44e1e..fbb4904 100644
--- a/tests/integration/test_verbosity.py
+++ b/tests/integration/test_verbosity.py
@@ -15,7 +15,7 @@
import pytest
from pretab import Preprocessor, PretabWarning, configure_logging, set_verbosity
-from pretab.exceptions import ConfigWarning
+from pretab.exceptions import ConfigWarning, DataWarning
@pytest.fixture
@@ -110,6 +110,16 @@ def test_verbose_2_logs_feature_table(sample_data, caplog):
assert "pipeline" in debug_text
+def test_verbose_2_lists_a_column_without_observed_values(sample_data, caplog):
+ X, y = sample_data
+ X = X.assign(empty=pd.Series([np.nan] * len(X), dtype=object))
+ caplog.set_level(logging.DEBUG, logger="pretab")
+ with pytest.warns(DataWarning, match="no observed"):
+ Preprocessor(numerical_method="ple", verbose=2).fit(X, y)
+ rows = [r.getMessage() for r in caplog.records if r.levelno == logging.DEBUG]
+ assert any(row.startswith("empty ") for row in rows)
+
+
def test_verbose_3_logs_internal_decisions(sample_data, caplog):
X, y = sample_data
caplog.set_level(logging.DEBUG, logger="pretab")