Feature Transformers API
centimators.feature_transformers
Feature transformers (in the scikit-learn sense) that integrate seamlessly with pipelines. Using metadata routing, centimators' transformers specialize in grouping features by a date or ticker series, and applying transformations to each group independently.
This module provides a family of stateless feature/target transformers built on top of
narwhals. Each class follows the sklearn.base.
TransformerMixin
interface which allows them to participate in
sklearn.pipeline.Pipeline
or ColumnTransformer
objects without extra
boiler-plate.
All transformers are fully vectorised, backend-agnostic (pandas, polars, …) and suitable for cross-validation, grid-search and other classic machine-learning workflows.
Highlights
- RankTransformer – converts numeric features into their (0, 1]-normalised rank within a user-supplied grouping column (e.g. a date).
- LagTransformer – creates shifted/lagged copies of features to expose temporal context for time-series models.
- MovingAverageTransformer – rolling mean across arbitrary window sizes.
- LogReturnTransformer – first-difference of the natural logarithm of a signal, a common way to compute returns.
- GroupStatsTransformer – horizontally aggregates arbitrary sets of columns and exposes statistics such as mean, standard deviation, skew, kurtosis, range and coefficient of variation.
RankTransformer
Bases: _BaseFeatureTransformer
RankTransformer transforms features into their normalized rank within groups defined by a date series.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feature_names
|
list of str
|
Names of columns to transform. If None, all columns of X are used. |
None
|
Examples:
>>> import pandas as pd
>>> from centimators.feature_transformers import RankTransformer
>>> df = pd.DataFrame({
... 'date': ['2021-01-01', '2021-01-01', '2021-01-02'],
... 'feature1': [3, 1, 2],
... 'feature2': [30, 20, 10]
... })
>>> transformer = RankTransformer(feature_names=['feature1', 'feature2'])
>>> result = transformer.fit_transform(df[['feature1', 'feature2']], date_series=df['date'])
>>> print(result)
feature1_rank feature2_rank
0 0.5 0.5
1 1.0 1.0
2 1.0 1.0
Source code in src/centimators/feature_transformers.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
transform(X, y=None, date_series=None)
Transforms features to their normalized rank.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
FrameT
|
Input data frame. |
required |
y
|
Any
|
Ignored. Kept for compatibility. |
None
|
date_series
|
IntoSeries
|
Series defining groups for ranking (e.g., dates). |
None
|
Returns:
Name | Type | Description |
---|---|---|
FrameT |
FrameT
|
Transformed data frame with ranked features. |
Source code in src/centimators/feature_transformers.py
get_feature_names_out(input_features=None)
Returns the output feature names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_features
|
list[str]
|
Ignored. Kept for compatibility. |
None
|
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: List of transformed feature names. |
Source code in src/centimators/feature_transformers.py
LagTransformer
Bases: _BaseFeatureTransformer
LagTransformer shifts features by specified lag windows within groups defined by a ticker series.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
windows
|
iterable of int
|
Lag periods to compute. Each feature will have shifted versions for each lag. |
required |
feature_names
|
list of str
|
Names of columns to transform. If None, all columns of X are used. |
None
|
Examples:
>>> import pandas as pd
>>> from centimators.feature_transformers import LagTransformer
>>> df = pd.DataFrame({
... 'ticker': ['A', 'A', 'A', 'B', 'B'],
... 'price': [10, 11, 12, 20, 21]
... })
>>> transformer = LagTransformer(windows=[1, 2], feature_names=['price'])
>>> result = transformer.fit_transform(df[['price']], ticker_series=df['ticker'])
>>> print(result)
price_lag1 price_lag2
0 NaN NaN
1 10.0 NaN
2 11.0 10.0
3 NaN NaN
4 20.0 NaN
Source code in src/centimators/feature_transformers.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
|
transform(X, y=None, ticker_series=None)
Applies lag transformation to the features.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
FrameT
|
Input data frame. |
required |
y
|
Any
|
Ignored. Kept for compatibility. |
None
|
ticker_series
|
IntoSeries
|
Series defining groups for lagging (e.g., tickers). |
None
|
Returns:
Name | Type | Description |
---|---|---|
FrameT |
FrameT
|
Transformed data frame with lagged features. Columns are ordered
by lag (as in |
Source code in src/centimators/feature_transformers.py
get_feature_names_out(input_features=None)
Returns the output feature names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_features
|
list[str]
|
Ignored. Kept for compatibility. |
None
|
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: List of transformed feature names, ordered by lag, then by feature. |
Source code in src/centimators/feature_transformers.py
MovingAverageTransformer
Bases: _BaseFeatureTransformer
MovingAverageTransformer computes the moving average of a feature over a specified window.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
windows
|
list of int
|
The windows over which to compute the moving average. |
required |
feature_names
|
list of str
|
The names of the features to compute the moving average for. |
None
|
Source code in src/centimators/feature_transformers.py
transform(X, y=None, ticker_series=None)
Applies moving average transformation to the features.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
FrameT
|
Input data frame. |
required |
y
|
Any
|
Ignored. Kept for compatibility. |
None
|
ticker_series
|
IntoSeries
|
Series defining groups for moving average (e.g., tickers). |
None
|
Returns:
Name | Type | Description |
---|---|---|
FrameT |
FrameT
|
Transformed data frame with moving average features. |
Source code in src/centimators/feature_transformers.py
get_feature_names_out(input_features=None)
Returns the output feature names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_features
|
list[str]
|
Ignored. Kept for compatibility. |
None
|
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: List of transformed feature names. |
Source code in src/centimators/feature_transformers.py
LogReturnTransformer
Bases: _BaseFeatureTransformer
LogReturnTransformer computes the log return of a feature.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feature_names
|
list of str
|
Names of columns to transform. If None, all columns of X are used. |
None
|
Source code in src/centimators/feature_transformers.py
transform(X, y=None, ticker_series=None)
Applies log return transformation to the features.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
FrameT
|
Input data frame. |
required |
y
|
Any
|
Ignored. Kept for compatibility. |
None
|
ticker_series
|
IntoSeries
|
Series defining groups for log return (e.g., tickers). |
None
|
Returns:
Name | Type | Description |
---|---|---|
FrameT |
FrameT
|
Transformed data frame with log return features. |
Source code in src/centimators/feature_transformers.py
get_feature_names_out(input_features=None)
Returns the output feature names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_features
|
list[str]
|
Ignored. Kept for compatibility. |
None
|
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: List of transformed feature names. |
Source code in src/centimators/feature_transformers.py
GroupStatsTransformer
Bases: _BaseFeatureTransformer
GroupStatsTransformer calculates statistical measures for defined feature groups.
This transformer computes mean, standard deviation, and skewness for each group of features specified in the feature_group_mapping.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feature_group_mapping
|
dict
|
Dictionary mapping group names to lists of feature columns. Example: {'group1': ['feature1', 'feature2'], 'group2': ['feature3', 'feature4']} |
required |
stats
|
list of str
|
List of statistics to compute for each group. If None, all statistics are computed. Valid options are 'mean', 'std', 'skew', 'kurt', 'range', and 'cv'. |
['mean', 'std', 'skew', 'kurt', 'range', 'cv']
|
Examples:
>>> import pandas as pd
>>> from centimators.feature_transformers import GroupStatsTransformer
>>> df = pd.DataFrame({
... 'feature1': [1, 2, 3],
... 'feature2': [4, 5, 6],
... 'feature3': [7, 8, 9],
... 'feature4': [10, 11, 12]
... })
>>> mapping = {'group1': ['feature1', 'feature2'], 'group2': ['feature3', 'feature4']}
>>> transformer = GroupStatsTransformer(feature_group_mapping=mapping)
>>> result = transformer.fit_transform(df)
>>> print(result)
group1_groupstats_mean group1_groupstats_std group1_groupstats_skew group2_groupstats_mean group2_groupstats_std group2_groupstats_skew
0 2.5 1.5 0.0 8.5 1.5 0.0
1 3.5 1.5 0.0 9.5 1.5 0.0
2 4.5 1.5 0.0 10.5 1.5 0.0
>>> transformer_mean_only = GroupStatsTransformer(feature_group_mapping=mapping, stats=['mean'])
>>> result_mean_only = transformer_mean_only.fit_transform(df)
>>> print(result_mean_only)
group1_groupstats_mean group2_groupstats_mean
0 2.5 8.5
1 3.5 9.5
2 4.5 10.5
Source code in src/centimators/feature_transformers.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
|
transform(X, y=None)
Calculates group statistics on the features.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
FrameT
|
Input data frame. |
required |
y
|
Any
|
Ignored. Kept for compatibility. |
None
|
Returns:
Name | Type | Description |
---|---|---|
FrameT |
FrameT
|
Transformed data frame with group statistics features. |
Source code in src/centimators/feature_transformers.py
get_feature_names_out(input_features=None)
Return feature names for all groups.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_features
|
list[str]
|
Ignored. Kept for compatibility. |
None
|
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: List of transformed feature names. |