Parcourir la source

Add functionality to ignore certain labels from being flattend

ogert il y a 4 ans
Parent
commit
02f1d2e464
1 fichiers modifiés avec 9 ajouts et 6 suppressions
  1. 9 6
      cdplib/FlattenData.py

+ 9 - 6
cdplib/FlattenData.py

@@ -20,7 +20,7 @@ class FlattenData():
     def __init__(self):
         self._log = Log("Flatten data")
     
-    def flatten(self, data, labels_to_ignore: list = None) -> pd.DataFrame():
+    def flatten(self, data, labels_to_ignore: list = []) -> pd.DataFrame():
         '''
         :parm data: data given in either dictionary, list or dataframe format.
         '''
@@ -50,7 +50,7 @@ class FlattenData():
         self._log.info(('Data has been flattened, created {} columns in {} seconds').format(len(result_dataframe.columns)- in_length, time.time()-start))
         return result_dataframe
 
-    def flatten_dataframe(self, dataframe: pd.DataFrame, incoming_key: str = None, labels_to_ignore: list = None):
+    def flatten_dataframe(self, dataframe: pd.DataFrame, incoming_key: str = None, labels_to_ignore: list = []):
         '''
         :param pd.Dataframe dataframe: dataframe containing the data to be flattened
         :param str incoming_key: string to be appended to the key
@@ -66,6 +66,9 @@ class FlattenData():
         for index, row in dataframe.iterrows():
             temp_result_dict = {}
             for key, value in row.iteritems():
+                print('key:',  key)
+                print('value:',  value)
+                print('labels_to_ignore:', labels_to_ignore)
                 if key not in labels_to_ignore:
                     temp_result = {}
                     if incoming_key is not None:
@@ -87,7 +90,7 @@ class FlattenData():
 
         return result_dict
 
-    def flatten_dict(self, dictionary: dict, incoming_key: str = None, labels_to_ignore: list = None):
+    def flatten_dict(self, dictionary: dict, incoming_key: str = None, labels_to_ignore: list = []):
         '''
         :param dict dictionary: dictionary containing the data to be flattened
         :param str incoming_key: string to be appended to the key
@@ -120,7 +123,7 @@ class FlattenData():
 
         return result_dict
 
-    def flatten_list(self, data_list: list, incoming_key: str = None, labels_to_ignore: list = None):
+    def flatten_list(self, data_list: list, incoming_key: str = None, labels_to_ignore: list = []):
         '''
         :param list data_list: list containing the data to be flattened
         :param str incoming_key: string to be appended to the key
@@ -180,11 +183,11 @@ class FlattenData():
 
         return dictionary
 
-    def flatten_if_not_flat(self, data: pd.DataFrame, labels_to_ignore: list = None):
+    def flatten_if_not_flat(self, data: pd.DataFrame, labels_to_ignore: list = []):
 
         for data_type in data.dtypes:
                 if data_type == object:
-                    return self.flatten(data) 
+                    return self.flatten(data, labels_to_ignore) 
 
         return data