reporting_nb_functions
frequenz.lib.notebooks.reporting.utils.reporting_nb_functions ¤
Utilities for analyzing and summarizing microgrid energy-flow data.
This module contains helper functions for transforming, aggregating, and summarizing power and energy data used throughout the reporting pipeline. It supports component-level analysis, overview table construction, energy summaries, and site-level metric aggregation.
Functions¤
-
build_component_analysis()Produce a tidy (long-format) DataFrame for a specific component type (e.g., Battery, PV, CHP), based on selected component IDs. -
build_overview_df()Select and return only the relevant reporting columns for overview plots, depending on available component types. -
compute_energy_summary()Compute relative distribution among production and grid consumption sources. -
aggregate_metrics()Compute high-level site metrics including production totals, self- consumption energy and share, battery-related flows, grid import and feed-in, and peak grid consumption with localized date.
Usage¤
These functions are typically applied to DataFrames produced by the normalized Energy Report pipeline. Input columns are assumed to represent instantaneous power measurements (in kW) sampled at a known fixed resolution. Energy values (kWh) are derived by multiplying power samples by the sampling interval.
Typical workflow:
1. Build an energy report DataFrame upstream (e.g., via create_energy_report_df).
2. Use build_overview_df to extract relevant columns for dashboards.
3. Use build_component_analysis to analyze per-component contributions.
4. Use compute_energy_summary to generate energy-mix tables.
5. Use aggregate_metrics to calculate site-wide KPIs such as production
totals, self-consumption share, and grid import peaks.
All missing or unavailable columns are treated safely (as zero-valued Series), ensuring resilient operation even with partially populated datasets.
Classes¤
Functions:¤
frequenz.lib.notebooks.reporting.utils.reporting_nb_functions.aggregate_metrics ¤
aggregate_metrics(
energy_report_df: DataFrame,
resolution: timedelta,
*,
tz_name: str = "Europe/Berlin",
price_column: str | None = None
) -> dict[str, float | None | str]
Aggregate key site-level energy and performance metrics from time-series data.
This function converts instantaneous power measurements (kW) into energy values (kWh) using the given sampling resolution and computes aggregated indicators across all major energy sources: PV, CHP, Wind, Battery, Grid, and total site consumption. It also evaluates self-consumption ratios and determines the peak grid consumption including its calendar date.
| PARAMETER | DESCRIPTION |
|---|---|
energy_report_df
|
DataFrame containing time-series power data (kW). Missing columns are
treated as zero. Expected canonical column names include:
-
TYPE:
|
resolution
|
Sampling interval between measurements (e.g.
TYPE:
|
tz_name
|
Timezone used when reporting the date of the peak grid consumption.
Defaults to
TYPE:
|
price_column
|
Name of the column containing the day-ahead price information.
Defaults to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, float | None | str]
|
dict[str, float | None | str]:
A dictionary of aggregated metrics including:
- |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Notes
- Missing columns are treated as zero-valued Series.
- Peak date is determined from the index label of the maximum grid import.
- Naive timestamps are assumed to be in UTC before timezone conversion.
Source code in src/frequenz/lib/notebooks/reporting/utils/reporting_nb_functions.py
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 | |
frequenz.lib.notebooks.reporting.utils.reporting_nb_functions.assemble_component_analysis ¤
assemble_component_analysis(
component_filter: list[str],
component_key: str,
component_types: list[str],
energy_report_df: DataFrame,
timestep_hours: float,
mapper: ColumnMapper,
component_label: str,
value_col_name: str,
invert_sign: bool = False,
trunc_values: bool = False,
mcfg: MicrogridConfig | None = None,
component_id_source: (
Literal["meter", "inverter"] | None
) = None,
) -> tuple[DataFrame, float, str]
Assemble a component-level analysis table and compute its energy total.
This function retrieves one or more component columns from the
Energy Report DataFrame (e.g., individual PV strings, batteries, CHP
units), converts them into long-form using build_component_analysis(),
applies display-name mapping, scales values by the timestep duration,
optionally inverts the sign, and returns both the transformed DataFrame
and the aggregated energy.
| PARAMETER | DESCRIPTION |
|---|---|
component_filter
|
List of component selectors. Can contain component numbers
(e.g. |
component_key
|
Component type key (e.g.
TYPE:
|
component_types
|
List of component types present in the Energy Report. |
energy_report_df
|
Source DataFrame containing timestamped component data.
Must include a
TYPE:
|
timestep_hours
|
Sampling interval expressed in hours (e.g.
TYPE:
|
mapper
|
ColumnMapper used to convert column names into display labels.
TYPE:
|
component_label
|
Human-readable label to inject into the melted output
(e.g.,
TYPE:
|
value_col_name
|
Name of the value column in the melted long-format DataFrame.
TYPE:
|
invert_sign
|
Whether to multiply results by
TYPE:
|
trunc_values
|
If
TYPE:
|
mcfg
|
Optional microgrid config used to resolve component IDs.
TYPE:
|
component_id_source
|
Optional ID source selector. Use
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
|
float
|
|
str
|
|
Notes
- If the component type is not present or the DataFrame lacks a
"timestamp"column, an empty result is returned.
Source code in src/frequenz/lib/notebooks/reporting/utils/reporting_nb_functions.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |
frequenz.lib.notebooks.reporting.utils.reporting_nb_functions.build_component_analysis ¤
build_component_analysis(
energy_report_df: DataFrame,
selection_filter: Iterable[str],
component_label: str,
value_col_name: str,
allowed_component_ids: set[str] | None = None,
) -> DataFrame
Build a long-format analysis table for a single component type.
Selects component columns such as "
| PARAMETER | DESCRIPTION |
|---|---|
energy_report_df
|
DataFrame containing timestamped component data with columns named in the
form "
TYPE:
|
selection_filter
|
Iterable defining which components to include: - If any entry equals "All" (case-insensitive), all matching component columns are selected. - Otherwise, entries should be component identifiers such as ["#1", "#3"]. |
component_label
|
The base label used in the component column names and in the resulting identifier column (e.g., "Battery", "CHP", "EV").
TYPE:
|
value_col_name
|
Name of the output column containing the selected component data (e.g., "battery", "chp", "ev").
TYPE:
|
allowed_component_ids
|
Optional set of component IDs to include in the analysis. |
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
pd.DataFrame:
A long-format DataFrame with columns:
- "timestamp"
- If no matching columns are found, returns an empty DataFrame with the appropriate columns. |
Source code in src/frequenz/lib/notebooks/reporting/utils/reporting_nb_functions.py
frequenz.lib.notebooks.reporting.utils.reporting_nb_functions.build_overview_df ¤
Build an overview table from a canonical energy report DataFrame.
The output always starts with the site-level columns timestamp,
grid_consumption, mid_consumption, and grid_feed_in when they
are available in energy_report_df. It then appends component-level
aggregate columns for the requested component types:
pv_asset_production, chp_asset_production,
wind_asset_production, battery_power_flow, and
battery_soc_pct when present.
If battery is present, the output is extended with battery plotting
helpers derived from the selected columns: peak_before_optimization,
peak_after_optimization, battery_charge, and
battery_discharge.
| PARAMETER | DESCRIPTION |
|---|---|
energy_report_df
|
Canonical energy report DataFrame.
TYPE:
|
component_types
|
Component type identifiers to include, e.g.
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
A copy of the selected overview columns plus battery helper columns when |
DataFrame
|
|
Source code in src/frequenz/lib/notebooks/reporting/utils/reporting_nb_functions.py
frequenz.lib.notebooks.reporting.utils.reporting_nb_functions.compute_energy_summary ¤
compute_energy_summary(
df: DataFrame,
resolution: timedelta,
include_rollups: bool = False,
drop_zeros: bool = True,
) -> DataFrame
Compute energy totals, average power, and percentage shares for key energy sources.
This function aggregates instantaneous power measurements (kW) over a fixed sampling interval to produce energy statistics (kWh) for major sources such as PV, wind, CHP, and grid consumption. It supports optional roll-ups of total on-site production and configurable filtering of near-zero results.
| PARAMETER | DESCRIPTION |
|---|---|
df
|
Input DataFrame containing instantaneous power columns in kW. Only the
following canonical columns are considered when present and numeric:
-
TYPE:
|
resolution
|
Sampling interval between observations (e.g.
TYPE:
|
include_rollups
|
If
TYPE:
|
drop_zeros
|
If
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
pd.DataFrame:
A summary table with one row per included energy source and columns:
- |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/frequenz/lib/notebooks/reporting/utils/reporting_nb_functions.py
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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | |