energy_fault_detector.data_preprocessing.timestamp_transformer

Since the EnergyFaultDetector not yet contains the TimestampTransformer it is added here as a separate script.

class TimestampTransformer(features=None, timestamp_col=None, groupby_level='auto')[source]

Bases: DataTransformer

A timestamp features generator for time-series data.

This transformer adds normalized time-derived features based on a DatetimeIndex or a dedicated timestamp column:

  • second_of_minute -> second / 60 in [0, 1)

  • minute_of_hour -> minute / 60 in [0, 1)

  • hour_of_day -> hour / 24 in [0, 1)

  • day_of_week -> weekday / 7 in [0, 1)

  • day_of_month -> day / days_in_month in [0, 1)

  • day_of_year -> dayofyear / 365/366 in [0, 1)

  • month_of_year -> month / 12 in [0, 1)

  • is_weekend -> 1 if Sat/Sun else 0

  • year -> calendar year (float)

This transformer is one-way; the inverse_transform returns the input unchanged. All features are normalized to the range [0, 1], except for year, which can be used to model drift. The features do not need to be scaled/normalized.

Parameters:
  • features (List[str]) –

    List of feature names to generate. Supported: [“second_of_minute”, “minute_of_hour”, “hour_of_day”, “day_of_week”, “day_of_month”, “month_of_year”,

    ”is_weekend”, “year”]

  • timestamp_col (Optional[str]) – The column name of the DataFrame containing timestamps. If None, the index is assumed to be the timestamp.

  • groupby_level (Optional[str]) – Optional index level name or position for grouping (e.g., ‘device_id’ or 0). If provided and a MultiIndex is used, timestamp features are extracted correctly per group. If ‘auto’, automatically detects the non-datetime level in a MultiIndex. Default: None (no grouping). For MultiIndex with more than one non-datetime level, it is recommended to set groupby_level explicitly.

Configuration example:

train:
  data_preprocessor:
    steps:
      - name: standard_scaler
      - name: timestamp_transformer
        params:
          features:
            - hour_of_day
            - day_of_week
            - month_of_year
__sklearn_is_fitted__()[source]

Check fitted status and return a Boolean value.

Returns:

True if the transformer has been fitted, False otherwise.

Return type:

bool

fit(x, y=None)[source]

Validate configuration and record feature names.

Parameters:
  • x (Union[ndarray, DataFrame]) – Input DataFrame with DatetimeIndex or a datetime64 timestamp column.

  • y (Optional[ndarray]) – Unused, present for compatibility.

Return type:

TimestampTransformer

get_feature_names_out(input_features=None)[source]

Returns the list of feature names in the output.

Return type:

List[str]

inverse_transform(x)[source]

We drop the added columns and return the original DataFrame.

Return type:

DataFrame

set_fit_request(*, x: bool | None | str = '$UNCHANGED$') TimestampTransformer

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for x parameter in fit.

Returns

selfobject

The updated object.

set_inverse_transform_request(*, x: bool | None | str = '$UNCHANGED$') TimestampTransformer

Request metadata passed to the inverse_transform method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to inverse_transform if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to inverse_transform.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for x parameter in inverse_transform.

Returns

selfobject

The updated object.

set_transform_request(*, x: bool | None | str = '$UNCHANGED$') TimestampTransformer

Request metadata passed to the transform method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to transform if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to transform.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

Parameters

xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for x parameter in transform.

Returns

selfobject

The updated object.

transform(x)[source]

Transforms the timestamp features from the given dataset.

Parameters:

x (DataFrame) – The data to transform.

Raises:

NotFittedError – Raised by check_is_fitted utility function if the object has not been fitted.

Returns:

Transformed dataset.

Return type:

DataFrame