Time series lag (translate) transformer.
Creates lagged (shifted) features from time series data.
Classes
Create lagged features from time-ordered data.
Shifts feature values by a specified number of periods. Useful for transforming time series data into a supervised learning format where past values are used to predict future ones.
Constructor
__init__( self, lag: int = ..., columns: list[int] | None = None, fill_with_missing: bool = True, invert_selection: bool = False, )
Parameters
lag
int
= -1
The number of periods to shift.
- •Negative values (e.g.,
-1): Use past values (most common). - •Positive values (e.g.,
1): Use future values (look-ahead).
columns
list of int
Indices of columns to shift. If
None, all columns are lagged.
fill_with_missing
bool
= True
- •If
True: Keeps the original number of rows and fills boundary
indices with np.nan.
- •If
False: Removes the rows that would containnp.nanresults.
invert_selection
bool
= False
If
True, applies the lag to all columns except those specified in columns.
Attributes
feature_names_out_
list of str
The generated names for the shifted features (e.g., "x-1").
Lag a feature by 1 period to use the previous day's value:
python
>>> from tuiml.preprocessing.timeseries import LagTransformer
>>> import numpy as np
>>> X = np.array([[10], [20], [30], [40]])
>>> lagger = LagTransformer(lag=-1)
>>> X_lagged = lagger.fit_transform(X)
>>> print(X_lagged.flatten())
[nan 10. 20. 30.]
Methods
get_parameter_schema
(cls)
fit
(self, X: np.ndarray, y: np.ndarray | None=None, feature_names: list[str] | None=None) -> 'LagTransformer'
fit
(self, X: np.ndarray, y: np.ndarray | None=None, feature_names: list[str] | None=None) -> 'LagTransformer'
Fit the transformer.
Parameters
X
np.ndarray of shape (n_samples, n_features)
Input data. Only its shape is recorded; no statistics are learned.
y
np.ndarray
Ignored, present for API consistency.
feature_names
list of str
Names of the input columns, used to label the generated columns.
Returns
self
object
The fitted transformer.
__repr__
(self) -> str