1212
1313import numpy as np
1414import pandas as pd
15+ from enum import Enum
1516import yaml
1617from power_grid_model import initialize_array
1718from power_grid_model .data_types import Dataset
@@ -145,9 +146,9 @@ def _parse_data(self, data: TabularData, data_type: str, extra_info: Optional[Ex
145146 def _convert_table_to_component ( # pylint: disable = too-many-arguments,too-many-positional-arguments
146147 self ,
147148 data : TabularData ,
148- data_type : str ,
149+ data_type : Union [ str , Enum ] ,
149150 table : str ,
150- component : str ,
151+ component : Union [ str , Enum ] ,
151152 attributes : InstanceAttributes ,
152153 extra_info : Optional [ExtraInfo ],
153154 ) -> Optional [np .ndarray ]:
@@ -165,9 +166,9 @@ def _convert_table_to_component( # pylint: disable = too-many-arguments,too-man
165166 extra_info: an optional dictionary where extra component info (that can't be specified in
166167 power-grid-model data) can be specified
167168 data: TabularData:
168- data_type: str:
169+ data_type: Union[ str, Enum] :
169170 table: str:
170- component: str:
171+ component: Union[ str, Enum] :
171172 attributes: InstanceAttributes:
172173 extra_info: Optional[ExtraInfo]:
173174
@@ -187,13 +188,16 @@ def _convert_table_to_component( # pylint: disable = too-many-arguments,too-man
187188
188189 n_records = np .sum (table_mask ) if table_mask is not None else len (data [table ])
189190
191+ component_str = component .value if isinstance (component , Enum ) else component
192+ data_type_str = data_type .value if isinstance (data_type , Enum ) else data_type
193+
190194 try :
191- pgm_data = initialize_array (data_type = data_type , component_type = component , shape = n_records )
195+ pgm_data = initialize_array (data_type = data_type_str , component_type = component_str , shape = n_records )
192196 except KeyError as ex :
193- raise KeyError (f"Invalid component type '{ component } ' or data type '{ data_type } '" ) from ex
197+ raise KeyError (f"Invalid component type '{ component_str } ' or data type '{ data_type_str } '" ) from ex
194198
195199 if "id" not in attributes :
196- raise KeyError (f"No mapping for the attribute 'id' for '{ component } s'!" )
200+ raise KeyError (f"No mapping for the attribute 'id' for '{ component_str } s'!" )
197201
198202 # Make sure that the "id" column is always parsed first (at least before "extra" is parsed)
199203 attributes_without_filter = {k : v for k , v in attributes .items () if k != "filters" }
@@ -207,7 +211,7 @@ def _convert_table_to_component( # pylint: disable = too-many-arguments,too-man
207211 data = data ,
208212 pgm_data = pgm_data ,
209213 table = table ,
210- component = component ,
214+ component = component_str ,
211215 attr = attr ,
212216 col_def = col_def ,
213217 table_mask = table_mask ,
@@ -232,7 +236,7 @@ def _convert_col_def_to_attribute( # pylint: disable = too-many-arguments,too-m
232236 data : TabularData ,
233237 pgm_data : np .ndarray ,
234238 table : str ,
235- component : str ,
239+ component : Union [ str , Enum ] ,
236240 attr : str ,
237241 col_def : Any ,
238242 table_mask : Optional [np .ndarray ],
@@ -254,7 +258,7 @@ def _convert_col_def_to_attribute( # pylint: disable = too-many-arguments,too-m
254258 data: TabularData:
255259 pgm_data: np.ndarray:
256260 table: str:
257- component: str:
261+ component: Union[ str, Enum] :
258262 attr: str:
259263 col_def: Any:
260264 extra_info: Optional[ExtraInfo]:
@@ -265,12 +269,15 @@ def _convert_col_def_to_attribute( # pylint: disable = too-many-arguments,too-m
265269 """
266270 # To avoid mistakes, the attributes in the mapping should exist. There is one extra attribute called
267271 # 'extra' in which extra information can be captured.
272+
273+ component_str = component .value if isinstance (component , Enum ) else component
274+
268275 if pgm_data .dtype .names is None :
269- raise ValueError (f"pgm_data for '{ component } s' has no attributes defined. (dtype.names is None)" )
276+ raise ValueError (f"pgm_data for '{ component_str } s' has no attributes defined. (dtype.names is None)" )
270277
271278 if attr not in pgm_data .dtype .names and attr not in ["extra" , "filters" ]:
272279 attrs = ", " .join (pgm_data .dtype .names )
273- raise KeyError (f"Could not find attribute '{ attr } ' for '{ component } s'. (choose from: { attrs } )" )
280+ raise KeyError (f"Could not find attribute '{ attr } ' for '{ component_str } s'. (choose from: { attrs } )" )
274281
275282 if attr == "extra" :
276283 # Extra info must be linked to the object IDs, therefore the uuids should be known before extra info can
0 commit comments