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, day_of_year, 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: scaler
        params:
          scaler_type: standard
      - name: timestamp_transformer
        params:
          features:
            - hour_of_day
            - day_of_week
            - month_of_year
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

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