From fed87128268d9e79c943f20b9b3656b808af0ef2 Mon Sep 17 00:00:00 2001 From: Marigold Date: Mon, 7 Oct 2024 16:53:52 +0200 Subject: [PATCH] :bug: convert percentages to numbers in fast-track --- apps/wizard/app_pages/fasttrack/process.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/apps/wizard/app_pages/fasttrack/process.py b/apps/wizard/app_pages/fasttrack/process.py index b634e12ed6f..8f78ac1f5ac 100644 --- a/apps/wizard/app_pages/fasttrack/process.py +++ b/apps/wizard/app_pages/fasttrack/process.py @@ -44,11 +44,13 @@ def processing_part_1(import_method, dataset_uris, infer_metadata, is_private, _ else: raise ValueError(f"Unknown import method: {import_method}") - # PROCES + # PROCESS if infer_metadata: st.write("Inferring metadata...") data, variables_meta_dict = _infer_metadata(data, variables_meta_dict) + data = _convert_percentages(data, variables_meta_dict) + # VALIDATION st.write("Validating data and metadata...") success = _validate_data(data, variables_meta_dict) @@ -145,6 +147,14 @@ def processing_part_2(data, dataset_meta, variables_meta_dict, origin, dataset_u return fast_import +def _convert_percentages(data: pd.DataFrame, variables_meta_dict: Dict[str, VariableMeta]) -> pd.DataFrame: + """Convert percentages to numbers.""" + for col in data.columns: + if getattr(variables_meta_dict.get(col, {}), "unit", "") == "%": + data[col] = data[col].str.replace("%", "").astype(float) + return data + + def _validate_data(df: pd.DataFrame, variables_meta_dict: Dict[str, VariableMeta]) -> bool: errors = []