helpers
frequenz.lib.notebooks.reporting.utils.helpers ¤
Energy flow and configuration utilities for microgrid analysis and reporting.
This module provides helper functions for
- Safe numeric extraction and aggregation from pandas DataFrames.
- Loading and validating YAML-based configuration files.
- Labeling microgrid component columns using configuration metadata.
- Computing derived energy flow metrics such as:
- Production excess
- Battery charge utilization
- Grid feed-in
- Self-consumption and self-share
- Formatting and timezone conversion utilities for reporting.
These utilities are primarily used in energy analytics pipelines and microgrid reporting notebooks to ensure consistent data preprocessing, metric calculation, and standardized output structures.
| FUNCTION | DESCRIPTION |
|---|---|
_get_numeric_series |
Safely extract a numeric Series or return zeros if missing. |
_sum_cols |
Safely sum multiple numeric columns. |
load_config |
Load and validate a YAML configuration file. |
_fmt_to_de_system |
Format numbers using German-style decimal conventions. |
_convert_timezone |
Convert a DataFrame timestamp column to a target timezone. |
label_component_columns |
Rename numeric component columns using MicrogridConfig. |
get_energy_report_columns |
Determine relevant columns for energy reporting. |
add_energy_flows |
Compute derived production, battery, and grid metrics. |
Notes
- These helpers are designed for internal use and assume well-structured DataFrames with datetime indices or timestamp columns.
- All numeric outputs are returned as float64 Series to ensure consistency.
Classes¤
Functions¤
frequenz.lib.notebooks.reporting.utils.helpers.add_energy_flows ¤
add_energy_flows(
df: DataFrame,
production_cols: list[str] | None = None,
consumption_cols: list[str] | None = None,
grid_cols: list[str] | None = None,
battery_cols: list[str] | None = None,
production_is_positive: bool = False,
) -> DataFrame
Compute and add derived energy flow metrics to the DataFrame.
This function aggregates production and consumption data, derives energy flow relationships such as grid feed-in, battery charging, and self-consumption, and appends these computed columns to the given DataFrame. Columns that are specified but missing or contain only null/zero values are ignored.
| PARAMETER | DESCRIPTION |
|---|---|
df
|
Input DataFrame containing production, consumption, and optionally battery power data.
TYPE:
|
production_cols
|
list of column names representing production sources. |
consumption_cols
|
list of column names representing consumption sources. |
grid_cols
|
list of column names representing grid import/export. |
battery_cols
|
optional list of column names for battery charging power. If None, battery-related flows are set to zero. |
production_is_positive
|
Whether production values are already positive.
If False,
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
A DataFrame including additional columns: - "production_excess": Production exceeding consumption. - "production_excess_in_bat": Portion of excess stored in the battery. - "grid_feed_in": Portion of excess fed into the grid. - "production_self_use": Self-consumed portion of production. - "production_self_share": Share of consumption covered by self-production. |
Source code in frequenz/lib/notebooks/reporting/utils/helpers.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 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 | |
frequenz.lib.notebooks.reporting.utils.helpers.build_color_map ¤
build_color_map(
cols: list[str],
color_dict: dict[str, str] | None = None,
palette: list[str] | None = None,
) -> dict[str, str]
Generate a color mapping for columns or categories.
Creates a mapping from column names (or categorical labels) to color
values. If user-specified colors are provided via color_dict, those
are applied first. Remaining columns are assigned distinct colors from
a chosen palette, ensuring no duplicates.
| PARAMETER | DESCRIPTION |
|---|---|
cols
|
List of column names or category labels to assign colors to. |
color_dict
|
Optional dictionary of pre-defined color mappings. Columns found here are assigned these colors directly. |
palette
|
Optional list of color codes to use as defaults. If None, a combined Plotly qualitative palette is used. |
| RETURNS | DESCRIPTION |
|---|---|
dict[str, str]
|
A dictionary mapping each column or category name to a unique color. |
Source code in frequenz/lib/notebooks/reporting/utils/helpers.py
frequenz.lib.notebooks.reporting.utils.helpers.convert_timezone ¤
Convert a datetime Series to a target timezone.
If the Series contains timezone-naive datetimes, they are first localized to
assume_tz before converting to target_tz.
| PARAMETER | DESCRIPTION |
|---|---|
ts
|
Input Series containing the datetime values.
TYPE:
|
target_tz
|
Timezone name to convert the Series to.
Defaults to
TYPE:
|
assume_tz
|
Timezone to assume for naive datetimes.
Defaults to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Series
|
pd.DataFrame: A copy of the DataFrame with the converted datetime column. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in frequenz/lib/notebooks/reporting/utils/helpers.py
frequenz.lib.notebooks.reporting.utils.helpers.fmt_to_de_system ¤
Format a number using German-style decimal and thousands separators.
The function formats the number with two decimal places, using a comma as the decimal separator and a dot as the thousands separator.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
The number to format.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The formatted string with German number formatting applied. |
Example
_fmt_to_de_system(12345.6789) '12.345,68'
Source code in frequenz/lib/notebooks/reporting/utils/helpers.py
frequenz.lib.notebooks.reporting.utils.helpers.get_energy_report_columns ¤
Build the list of dataframe columns for the energy report.
The selected columns depend on the available component types.
| PARAMETER | DESCRIPTION |
|---|---|
component_types
|
List of component types (e.g. ["pv", "battery"]) |
single_components
|
Extra component columns to always include. |
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
The full list of dataframe columns. |
Source code in frequenz/lib/notebooks/reporting/utils/helpers.py
frequenz.lib.notebooks.reporting.utils.helpers.label_component_columns ¤
label_component_columns(
df: DataFrame,
mcfg: MicrogridConfig,
column_battery: str = "battery",
column_pv: str = "pv",
column_chp: str = "chp",
column_ev: str = "ev",
) -> tuple[DataFrame, list[str]]
Rename numeric single-component columns to labeled names.
Numeric string column names like "14" are converted to
"Battery #14", "PV #14", "CHP #14" or "EV #14" based on
the component IDs provided by mcfg.component_type_ids(...)
| PARAMETER | DESCRIPTION |
|---|---|
df
|
Input DataFrame with numeric string column names.
TYPE:
|
mcfg
|
Configuration with
TYPE:
|
column_battery
|
Key name for battery component type.
TYPE:
|
column_pv
|
Key name for PV component type.
TYPE:
|
column_chp
|
Key name for CHP component type.
TYPE:
|
column_ev
|
Key name for EV component type
TYPE:
|
Returns: Tuple containing the renamed DataFrame and the list of applied labels
Source code in frequenz/lib/notebooks/reporting/utils/helpers.py
frequenz.lib.notebooks.reporting.utils.helpers.load_config ¤
Load a YAML config file and return it as a dictionary.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
Path to the YAML file.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
Configuration values as a dictionary. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the YAML root element is not a mapping (dict). |
Source code in frequenz/lib/notebooks/reporting/utils/helpers.py
frequenz.lib.notebooks.reporting.utils.helpers.long_to_wide ¤
long_to_wide(
df: DataFrame,
*,
time_col: str | Index = "Timestamp",
category_col: str | None = "Battery",
value_col: str | None = "Battery Throughput",
sum_col_name: str | None = None,
aggfunc: str = "sum"
) -> DataFrame
Convert a long-format DataFrame into wide format with optional aggregation.
Transforms a long-format dataset (one row per timestamp-category pair) into a wide-format table, where each category becomes a separate column. Optionally adds a total (sum) column across all categories.
| PARAMETER | DESCRIPTION |
|---|---|
df
|
Input DataFrame in long format.
TYPE:
|
time_col
|
Column name representing timestamps used as the index in
the resulting wide table. Defaults to
TYPE:
|
category_col
|
Column name representing category labels that become
column headers in the wide table. Defaults to
TYPE:
|
value_col
|
Column name representing numeric values to aggregate and
pivot into columns. Defaults to
TYPE:
|
sum_col_name
|
Optional name for a new column containing the row-wise sum
of all category columns. If None, defaults to
TYPE:
|
aggfunc
|
Aggregation function applied when multiple entries exist per
timestamp-category pair (e.g.,
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
A wide-format DataFrame with one row per timestamp, one column per category, |
DataFrame
|
and an optional total column representing the aggregated sum across all categories. |
Source code in frequenz/lib/notebooks/reporting/utils/helpers.py
frequenz.lib.notebooks.reporting.utils.helpers.set_date_to_midnight ¤
Return a timezone-aware datetime set to midnight of the given date.
Converts a date or datetime into a midnight timestamp localized to the specified timezone. If the input is already a datetime, only the date portion is used.
| PARAMETER | DESCRIPTION |
|---|---|
input_date
|
Date or datetime object to normalize to midnight. |
timezone_name
|
Name of the target timezone (e.g., "Europe/Berlin"). Defaults to "UTC". Falls back to UTC if the timezone name is invalid.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
datetime
|
A timezone-aware datetime object representing midnight of the |
datetime
|
given date in the specified timezone. |