|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2025 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Microgrid Reporting DataFrame Construction. |
| 5 | +
|
| 6 | +This module constructs normalized energy-report DataFrames from |
| 7 | +raw microgrid telemetry by harmonizing timestamps and column naming, |
| 8 | +enriching PV flows, adding grid KPIs, and surfacing component-specific |
| 9 | +metrics used downstream for dashboards. |
| 10 | +
|
| 11 | +Key Features |
| 12 | +------------ |
| 13 | +- Energy Report DataFrame Construction |
| 14 | + - :func:`create_energy_report_df`: Builds a normalized energy report table with |
| 15 | + unified naming, timezone conversion, grid import calculation, and |
| 16 | + component renaming based on a MicrogridConfig. |
| 17 | +
|
| 18 | +Usage |
| 19 | +----- |
| 20 | +Use create_energy_report_dfs() inside reporting pipelines or notebooks to |
| 21 | +transform raw microgrid exports into localized, labeled, and analysis-ready |
| 22 | +tables for KPIs, dashboards, and stakeholder reporting. |
| 23 | +""" |
| 24 | + |
| 25 | + |
| 26 | +from typing import List |
| 27 | + |
| 28 | +import pandas as pd |
| 29 | + |
| 30 | +from frequenz.data.microgrid.config import MicrogridConfig |
| 31 | +from frequenz.lib.notebooks.reporting.utils.column_mapper import ColumnMapper |
| 32 | +from frequenz.lib.notebooks.reporting.utils.helpers import ( |
| 33 | + _add_pv_energy_flows, |
| 34 | + _convert_timezone, |
| 35 | + add_net_grid_import, |
| 36 | + get_energy_report_columns, |
| 37 | + label_component_columns, |
| 38 | +) |
| 39 | + |
| 40 | + |
| 41 | +# pylint: disable=too-many-arguments, disable=too-many-locals |
| 42 | +def create_energy_report_dfs( |
| 43 | + df: pd.DataFrame, |
| 44 | + component_types: List[str], |
| 45 | + mcfg: MicrogridConfig, |
| 46 | + mapper: ColumnMapper, |
| 47 | + *, |
| 48 | + tz_name: str = "Europe/Berlin", |
| 49 | + assume_tz: str = "UTC", |
| 50 | +) -> pd.DataFrame: |
| 51 | + """Create a normalized Energy Report DataFrame with selected columns. |
| 52 | +
|
| 53 | + Makes a copy of the input, converts the timestamp column to the configured |
| 54 | + timezone, renames standard columns to unified names, adds the net import |
| 55 | + column, renames numeric component IDs to labeled names, and returns a |
| 56 | + reduced DataFrame containing only relevant columns. |
| 57 | +
|
| 58 | + Args: |
| 59 | + df: Raw input table containing energy data. |
| 60 | + component_types: Component types to include in the Energy Report DataFrame |
| 61 | + (e.g., ``battery``, ``pv``). |
| 62 | + mcfg: Configuration object used to resolve component IDs. |
| 63 | + mapper: Column Mapper object to standardize the column names. |
| 64 | + tz_name: Target timezone name for timestamp conversion (default: "Europe/Berlin"). |
| 65 | + assume_tz: Timezone to assume for naive datetimes before conversion (default: "UTC"). |
| 66 | +
|
| 67 | + Returns: |
| 68 | + The Energy Report DataFrame with standardized and selected columns. |
| 69 | +
|
| 70 | + Notes: |
| 71 | + Component IDs are renamed to labeled names via ``label_component_columns()``. |
| 72 | + """ |
| 73 | + energy_report_df = df.copy() |
| 74 | + |
| 75 | + # Only reset index if it's a datetime or period index and 'timestamp' column is missing |
| 76 | + if isinstance(energy_report_df.index, (pd.DatetimeIndex, pd.PeriodIndex)): |
| 77 | + if "timestamp" not in energy_report_df.columns: |
| 78 | + energy_report_df = energy_report_df.reset_index(names="timestamp") |
| 79 | + |
| 80 | + # Add PV energy flow columns |
| 81 | + energy_report_df = _add_pv_energy_flows(energy_report_df) |
| 82 | + |
| 83 | + # Standardize column names (from raw to canonical) |
| 84 | + energy_report_df = mapper.to_canonical(energy_report_df) |
| 85 | + |
| 86 | + # Convert timezone |
| 87 | + energy_report_df = _convert_timezone( |
| 88 | + energy_report_df, |
| 89 | + column_timestamp="timestamp", |
| 90 | + target_tz=tz_name, |
| 91 | + assume_tz=assume_tz, |
| 92 | + ) |
| 93 | + |
| 94 | + # Add grid consumption column |
| 95 | + energy_report_df = add_net_grid_import( |
| 96 | + energy_report_df, |
| 97 | + column_grid="grid", |
| 98 | + column_net_import="net_import", |
| 99 | + ) |
| 100 | + |
| 101 | + # Helper to rename numeric component IDs to labeled names like PV #250, Battery #219 |
| 102 | + energy_report_df, single_components = label_component_columns( |
| 103 | + energy_report_df, |
| 104 | + mcfg, |
| 105 | + column_battery="battery", |
| 106 | + column_pv="pv", |
| 107 | + ) |
| 108 | + |
| 109 | + energy_report_df_cols = get_energy_report_columns( |
| 110 | + component_types, single_components |
| 111 | + ) |
| 112 | + |
| 113 | + # Select only the relevant columns |
| 114 | + energy_report_df = energy_report_df[energy_report_df_cols] |
| 115 | + return energy_report_df |
0 commit comments