preprocessing.imputation.WeightFunction

preprocessing.imputation.WeightFunction(weights_series)

Callable class for sample weights that can be pickled.

This class wraps the weights_series and provides a callable interface compatible with ForecasterRecursive’s weight_func parameter. Unlike local functions with closures, instances of this class can be pickled using standard pickle/joblib.

When all weights for the requested index sum to zero (e.g. the entire training window falls inside gap-penalty zones after outlier detection), __call__ returns None instead of an all-zero array. ForecasterRecursive.create_sample_weights treats a None return as “no weighting”, avoiding a ValueError while still applying the weighted imputation for windows that do contain positive weights.

Parameters

Name Type Description Default
weights_series pd.Series Series containing weight values for each index. required

Examples

>>> import pandas as pd
>>> import pickle
>>> weights = pd.Series([1.0, 0.9, 0.8], index=[0, 1, 2])
>>> weight_func = WeightFunction(weights)
>>> weight_func(pd.Index([0, 1]))
array([1. , 0.9])
>>> # Returns None when all weights in the window are zero
>>> zero_weights = pd.Series([0.0, 0.0, 0.0], index=[0, 1, 2])
>>> wf_zero = WeightFunction(zero_weights)
>>> wf_zero(pd.Index([0, 1])) is None
True
>>> # Can be pickled
>>> pickled = pickle.dumps(weight_func)
>>> unpickled = pickle.loads(pickled)
>>> unpickled(pd.Index([0, 1]))
array([1. , 0.9])