column_mapper
frequenz.lib.notebooks.reporting.utils.column_mapper ¤
Column mapping utilities for energy reporting.
Keeps reporting notebooks insulated from upstream schema churn
by loading the canonical schema from
src/frequenz/lib/notebooks/reporting/schema_mapping.yaml. The class translates
raw headers to canonical names, resolves locale-aware display labels.
Raw names are the column labels emitted directly by the reporting API, canonical
names are the identifiers used throughout this codebase, and the display names
(en/de) expose English and German labels rendered in the notebooks. The
mapper is designed to work hand-in-hand with the schema_mapping.yaml file so that
every canonical column carries consistent units and descriptions.
Examples:
>>> from frequenz.lib.notebooks.reporting.utils import ColumnMapper
>>> mapper = ColumnMapper.from_yaml(
... "src/frequenz/lib/notebooks/reporting/schema_mapping.yaml",
... locale="de",
... )
>>> display_df = mapper.to_display(mapper.to_canonical(raw_df))
Classes¤
frequenz.lib.notebooks.reporting.utils.column_mapper.ColumnMapper
dataclass
¤
Column schema with locale-aware display labels.
Source code in frequenz/lib/notebooks/reporting/utils/column_mapper.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 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 | |
Attributes¤
canonical_to_display
property
¤
Resolved display labels for the current locale (with fallback).
canonicals
property
¤
Return the canonical column names defined in this mapper.
Functions¤
from_default
classmethod
¤
from_default(
*,
locale: str = "de",
fallback_locale: str = "en",
required: Iterable[str] | None = None
) -> ColumnMapper
Create an instance using the built-in default YAML resource.
Loads the default configuration file bundled with the package and initializes the class using localized settings.
| PARAMETER | DESCRIPTION |
|---|---|
locale
|
Primary locale code for localized configuration values. Defaults to "de".
TYPE:
|
fallback_locale
|
Secondary locale used if a key is missing in the primary locale. Defaults to "en".
TYPE:
|
required
|
Optional list of keys that must be present in the loaded configuration. If provided and any are missing, an exception is raised. |
| RETURNS | DESCRIPTION |
|---|---|
ColumnMapper
|
An initialized instance of the class populated from the |
ColumnMapper
|
built-in default YAML resource. |
Source code in frequenz/lib/notebooks/reporting/utils/column_mapper.py
from_yaml
classmethod
¤
from_yaml(
path: str,
*,
locale: str = "de",
fallback_locale: str = "en",
required: Iterable[str] | None = None
) -> "ColumnMapper"
Create a ColumnMapper from a YAML configuration file.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
Path to the YAML file containing the column mapping definition.
TYPE:
|
locale
|
Preferred display locale (default: "de").
TYPE:
|
fallback_locale
|
Fallback locale if the preferred one is missing (default: "en").
TYPE:
|
required
|
Optional list of required canonical column names. Raises ValueError if any are missing. |
| RETURNS | DESCRIPTION |
|---|---|
'ColumnMapper'
|
A ColumnMapper instance built from the YAML configuration. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the YAML is missing required fields or contains invalid mappings. |
Source code in frequenz/lib/notebooks/reporting/utils/column_mapper.py
to_canonical ¤
Rename incoming raw headers to canonical headers (normalize once).
to_display ¤
Rename canonical headers to localized display labels.
to_raw ¤
with_locale ¤
Create a copy with a different display locale (no re-read of YAML).