From 026708e72bc246c9a7abcf401bb4e7ea80b30295 Mon Sep 17 00:00:00 2001
From: tm689 <tm689@cam.ac.uk>
Date: Wed, 10 May 2023 12:13:35 +0100
Subject: [PATCH] Feat: add parse_cases function. New function to allow
 conversion of a series according to a provided dictionary. (Needed for WRT
 formating.)

---
 coordinator/ProcessorSurveyUtils.py | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/coordinator/ProcessorSurveyUtils.py b/coordinator/ProcessorSurveyUtils.py
index bfa5a8e..4da2ecc 100644
--- a/coordinator/ProcessorSurveyUtils.py
+++ b/coordinator/ProcessorSurveyUtils.py
@@ -74,11 +74,23 @@ def parse_date(series,name_out='date',fmt_in = '%Y-%m-%d',fmt_out= '%b %d, %Y'):
 
     return s_out
 
+def parse_cases(series, name_out, cases, dtype = None):
+    if dtype is None:
+        dtype = series.dtype
+    # Converting entries according to a cases dictionary
+    series_out = series.map(cases).astype(dtype)
+    
+    # Renaming series
+    series_out.rename(name_out,inplace=True)
+
+    return series_out
+
 # dict of functions callable within coln_parser_dict
 # so they can be obtained with a string in coln_parser_dict
 func_dict = {
     'parse_date' : parse_date,
-    'parse_location_kobotoolbox' : parse_location_kobotoolbox
+    'parse_location_kobotoolbox' : parse_location_kobotoolbox,
+    'parse_cases' : parse_cases
 }
 
 def parse_columns(df_in,coln_parser_dict):
-- 
GitLab