Skip to main content
Version: 0.0.98

ts_shape.events.energy.consumption_analysis

Classes
NameDescription
EnergyConsumptionEventsEnergy: Consumption Analysis

EnergyConsumptionEvents​

classsource
EnergyConsumptionEvents(
dataframe: pd.DataFrame,
*,
event_uuid: str = "energy:consumption",
time_column: str = "systime",
uuid_column: str = "uuid"
) -> None

Bases: Base

Energy: Consumption Analysis

Analyze energy consumption patterns from meter/sensor signals.

Values are expected to be energy deltas in kWh (consumption since the previous reading). For an instantaneous power signal in kW, integrate it with :class:~ts_shape.transform.calculator.power_integration.PowerIntegrator first -- summing kW samples measures the logging rate, not the energy.

Supports two data models via constructor parameters:

  • Standard (ts-shape default)::

    EnergyConsumptionEvents(df)

    expects: systime | uuid | value_double

  • Raw CSV (time + id + value)::

    EnergyConsumptionEvents(df, time_column="time", uuid_column="id")

    expects: time | id | value

    pass value_column="value" to each method

Methods:

  • consumption_by_window: Aggregate energy per time window from a meter UUID.
  • peak_demand_detection: Flag windows where consumption exceeds a threshold.
  • consumption_baseline_deviation: Compare actual vs rolling baseline.
  • energy_per_unit: Energy per production unit when paired with a counter.
  • normalize: Static helper to convert raw CSV format to standard schema.
Example
>>> import ts_shape
>>> df = ts_shape.make_timeseries(["meter"], n_points=96, freq="15min",
... baseline=5.0, noise=0.5) # kWh per 15 min
>>> energy = ts_shape.EnergyConsumptionEvents(df)
>>> out = energy.consumption_by_window("meter", window="6h")
>>> out[["start", "consumption"]].round({"consumption": 1})
start consumption
0 2025-01-01 00:00:00 119.8
1 2025-01-01 06:00:00 122.1
2 2025-01-01 12:00:00 118.9
3 2025-01-01 18:00:00 118.1
Functions
NameDescription
normalizeConvert a raw energy DataFrame to the standard ts-shape schema.
consumption_by_windowAggregate energy consumption per time window.
peak_demand_detectionDetect peak demand periods exceeding a threshold.
consumption_baseline_deviationCompare actual consumption vs rolling baseline.
energy_per_unitCalculate energy consumption per production unit.
Parameters
NameTypeDescriptionDefault
dataframeDataFrameLong-format timeseries frame (one row per sample, uuid identifies the signal).required
event_uuidstrUUID stamped on emitted events.'energy:consumption'
time_columnstrTimestamp column.'systime'
uuid_columnstrColumn identifying the signal (e.g. "id" for a raw CSV frame).'uuid'

normalize​

staticmethodsource
normalize(
dataframe: pd.DataFrame,
*,
series_id: str,
time_column: str = "time",
value_column: str = "value",
uuid_column: str | None = None
) -> pd.DataFrame

Convert a raw energy DataFrame to the standard ts-shape schema.

Not the library-wide normalization (:func:ts_shape.utils.normalize.normalize_signal_frame): this reads a raw two-column export (time / value by default) and returns the long systime / uuid / value_double frame the detectors take. A frame that is already long format needs no call.

Handles two input formats:

  • Two-column CSV: (time, value) — series_id is assigned as uuid.
  • Three-column with explicit id: (time, uuid_column, value) — values from uuid_column are used as uuid; series_id is ignored.
Parameters
NameTypeDescriptionDefault
dataframeDataFrameRaw DataFrame.required
series_idstrUUID to assign when no uuid_column is provided.required
time_columnstrName of the timestamp column in dataframe.'time'
value_columnstrName of the value column in dataframe.'value'
uuid_columnstr | NoneOptional column name whose values become the uuid.None
Returns
TypeDescription
DataFrameDataFrame with columns: systime, uuid, value_double, is_delta

consumption_by_window​

methodsource
consumption_by_window(
meter_uuid: str,
*,
value_column: str = "value_double",
window: str = "1h",
agg: str = "sum"
) -> pd.DataFrame

Aggregate energy consumption per time window.

Parameters
NameTypeDescriptionDefault
meter_uuidstrUUID of the energy meter signal.required
value_columnstrColumn containing energy readings.'value_double'
windowstrResample window (e.g. '1h', '15min', '1D').'1h'
aggstrAggregation method ('sum', 'mean', 'max').'sum'
Returns
NameTypeDescription
DataFrameDataFramestart, end, duration_seconds, uuid, source_uuid, is_delta, consumption. end is start plus window.

peak_demand_detection​

methodsource
peak_demand_detection(
meter_uuid: str,
*,
value_column: str = "value_double",
window: str = "15min",
threshold: float | None = None,
percentile: float = 0.95
) -> pd.DataFrame

Detect peak demand periods exceeding a threshold.

If threshold is None, uses the given percentile of windowed consumption.

Parameters
NameTypeDescriptionDefault
meter_uuidstrUUID of the energy meter signal.required
value_columnstrColumn containing energy readings.'value_double'
windowstrResample window for demand calculation.'15min'
thresholdfloat | NoneAbsolute demand threshold. If None, auto-calculated.None
percentilefloatPercentile to use for auto-threshold (default 95th).0.95
Returns
NameTypeDescription
DataFrameDataFramestart, end, duration_seconds, uuid, source_uuid, is_delta, demand, threshold, is_peak

consumption_baseline_deviation​

methodsource
consumption_baseline_deviation(
meter_uuid: str,
*,
value_column: str = "value_double",
window: str = "1h",
baseline_periods: int = 24,
deviation_threshold: float = 0.2
) -> pd.DataFrame

Compare actual consumption vs rolling baseline.

Parameters
NameTypeDescriptionDefault
meter_uuidstrUUID of the energy meter signal.required
value_columnstrColumn containing energy readings.'value_double'
windowstrResample window for consumption.'1h'
baseline_periodsintNumber of windows for rolling baseline.24
deviation_thresholdfloatFractional deviation to flag (0.2 = 20%).0.2
Returns
NameTypeDescription
DataFrameDataFramestart, end, duration_seconds, uuid, source_uuid, is_delta, consumption, baseline, deviation_pct, is_anomaly

energy_per_unit​

methodsource
energy_per_unit(
meter_uuid: str,
counter_uuid: str,
*,
value_column_energy: str = "value_double",
value_column_counter: str = "value_integer",
window: str = "1h",
handle_resets: bool = True
) -> pd.DataFrame

Calculate energy consumption per production unit.

Parameters
NameTypeDescriptionDefault
meter_uuidstrUUID of the energy meter signal.required
counter_uuidstrUUID of the production counter signal.required
value_column_energystrColumn with energy readings.'value_double'
value_column_counterstrColumn with counter readings.'value_integer'
windowstrTime window for aggregation.'1h'
handle_resetsboolTrue (default) treats a decreasing production counter as a reset and sums reset-aware increments per window (see :func:ts_shape.utils.counters.counter_per_window), so a window with a reset still reports what was produced. False uses the per-window max - min.True
Returns
NameTypeDescription
DataFrameDataFramestart, end, duration_seconds, uuid, source_uuid, is_delta, energy, units_produced, energy_per_unit