Transformer that applies linear interpolation to time series data.
The transformer always runs y.interpolate(method="linear") first. The on_missing keyword then chooses how to handle any NaN that linear interpolation cannot bridge (typically the leading and trailing endpoints of the series).
Parameters
Name
Type
Description
Default
on_missing
OnMissing
Contract for residual NaN after linear interpolation. - "raise" (default, fail-safe): raise ValueError if any NaN remains. Refuses to silently embed imputed values disguised as measurements. - "ffill_bfill": explicit opt-in to endpoint forward-then-backward fill, so that leading and trailing NaNs are both bridged. - "passthrough": return the linearly interpolated series unchanged. The caller promises to handle the residual NaN downstream.
If on_missing is not one of the three accepted values, or if on_missing="raise" (the default) and any NaN remains after linear interpolation.
Examples
import numpy as npimport pandas as pdfrom spotforecast2_safe.preprocessing.linearly_interpolate_ts import LinearlyInterpolateTS# Interior gaps are bridged by linear interpolation under every# mode; the default "raise" then succeeds because nothing remains.s = pd.Series([1.0, np.nan, 3.0])result = LinearlyInterpolateTS().fit_transform(s)print(result.tolist())assert result.tolist() == [1.0, 2.0, 3.0]
[1.0, 2.0, 3.0]
import numpy as npimport pandas as pdfrom spotforecast2_safe.preprocessing.linearly_interpolate_ts import LinearlyInterpolateTS# Endpoint NaNs are bridged only when the caller opts in explicitly.s = pd.Series([1.0, np.nan, 3.0, np.nan])result = LinearlyInterpolateTS(on_missing="ffill_bfill").fit_transform(s)print(result.tolist())assert result.tolist() == [1.0, 2.0, 3.0, 3.0]
[1.0, 2.0, 3.0, 3.0]
import numpy as npimport pandas as pdfrom spotforecast2_safe.preprocessing.linearly_interpolate_ts import LinearlyInterpolateTS# "passthrough" lets the residual NaN survive for the caller to handle.s = pd.Series([1.0, np.nan, 3.0, np.nan])out = LinearlyInterpolateTS(on_missing="passthrough").fit_transform(s)print(out.isna().tolist())assertbool(out.isna().iloc[-1]) isTrue
residual NaN handled according to self.on_missing.
Examples
import numpy as npimport pandas as pdfrom spotforecast2_safe.preprocessing.linearly_interpolate_ts import LinearlyInterpolateTS# Fit-then-transform mirrors the canonical sklearn workflow.idx = pd.date_range("2024-01-01", periods=4, freq="h")df = pd.DataFrame({"load_mw": [100.0, np.nan, 120.0, 130.0]}, index=idx)interp = LinearlyInterpolateTS()interp.fit(df)df_out = interp.transform(df)print(df_out)assert df_out["load_mw"].iloc[1] ==110.0, "interior NaN must be filled"assertnot df_out.isna().any().any(), "no NaN must remain"