preprocessing.linearly_interpolate_ts

preprocessing.linearly_interpolate_ts

Linear interpolation transformer for time series data.

Classes

Name Description
LinearlyInterpolateTS Transformer that applies linear interpolation to time series data.

LinearlyInterpolateTS

preprocessing.linearly_interpolate_ts.LinearlyInterpolateTS(on_missing='raise')

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. 'raise'

Raises

Name Type Description
ValueError 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 pandas as pd
>>> import numpy as np
>>> from 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])
>>> LinearlyInterpolateTS().fit_transform(s).tolist()
[1.0, 2.0, 3.0]

Endpoint NaNs are bridged only when the caller opts in explicitly:

>>> s = pd.Series([1.0, np.nan, 3.0, np.nan])
>>> LinearlyInterpolateTS(on_missing="ffill_bfill").fit_transform(s).tolist()
[1.0, 2.0, 3.0, 3.0]

"passthrough" lets the residual NaN survive for the caller to handle:

>>> out = LinearlyInterpolateTS(on_missing="passthrough").fit_transform(s)
>>> bool(out.isna().iloc[-1])
True

Methods

Name Description
apply Apply linear interpolation and dispatch on on_missing.
fit Fitted transformer (no-op).
transform Transform the input data by applying linear interpolation.
apply
preprocessing.linearly_interpolate_ts.LinearlyInterpolateTS.apply(y)

Apply linear interpolation and dispatch on on_missing.

Parameters
Name Type Description Default
y Union[pd.Series, pd.DataFrame] Input Series or DataFrame. required
Returns
Name Type Description
Union[pd.Series, pd.DataFrame] Union[pd.Series, pd.DataFrame]: The transformed data. For
Union[pd.Series, pd.DataFrame] on_missing="ffill_bfill" any residual endpoint NaN has
Union[pd.Series, pd.DataFrame] been forward- and back-filled; for "passthrough" it
Union[pd.Series, pd.DataFrame] survives; for "raise" (the default) the method raises
Union[pd.Series, pd.DataFrame] instead of returning a NaN-bearing result.
Raises
Name Type Description
ValueError If self.on_missing is not a recognized value, or if self.on_missing == "raise" and any NaN remains after linear interpolation.
fit
preprocessing.linearly_interpolate_ts.LinearlyInterpolateTS.fit(X, y=None)

Fitted transformer (no-op).

Parameters
Name Type Description Default
X Any Input data. required
y Any Ignored. None
Returns
Name Type Description
self LinearlyInterpolateTS The fitted transformer.
transform
preprocessing.linearly_interpolate_ts.LinearlyInterpolateTS.transform(X)

Transform the input data by applying linear interpolation.

Parameters
Name Type Description Default
X Union[pd.Series, pd.DataFrame] Input Series or DataFrame to interpolate. required
Returns
Name Type Description
Union[pd.Series, pd.DataFrame] Union[pd.Series, pd.DataFrame]: Interpolated data, with
Union[pd.Series, pd.DataFrame] residual NaN handled according to self.on_missing.