Quellcode durchsuchen

Changed flatten to always return dataframe

ogert vor 5 Jahren
Ursprung
Commit
18170aa33f
2 geänderte Dateien mit 11 neuen und 7 gelöschten Zeilen
  1. 9 5
      cdplib/FlattenData.py
  2. 2 2
      cdplib/db_handlers/MongodbHandler.py

+ 9 - 5
cdplib/FlattenData.py

@@ -31,12 +31,17 @@ class FlattenData():
         start = time.time()
         if type(data) is pd.DataFrame:
             return_data = self.flatten_dataframe(data)
-            self._log.info(('Data has been flattened, created {} columns in {} seconds').format(len(return_data.columns)- len(data.columns), time.time()-start))
-            return return_data
         if type(data) is dict:
-            return self.flatten_dict(data)
+            return_data = self.flatten_dict(data)
         if type(data) is list:
-            return self.flatten_list(data)
+            return_data =  self.flatten_list(data)
+        else:
+            self._log.log_and_raise_warning('Input data type is not supported')
+            return None
+
+        result_dataframe = pd.DataFrame.from_dict(return_data, orient='index')
+        self._log.info(('Data has been flattened, created {} columns in {} seconds').format(len(return_data.columns)- len(data.columns), time.time()-start))
+        return result_dataframe
 
     def flatten_dataframe(self, dataframe: pd.DataFrame, incoming_key: str = None):
         '''
@@ -68,7 +73,6 @@ class FlattenData():
 
             result_dict[index] = copy.deepcopy(temp_result_dict)
 
-        result_dataframe = pd.DataFrame.from_dict(result_dict, orient='index')
         return result_dataframe
 
     def flatten_dict(self, dictionary: dict, incoming_key: str = None):

+ 2 - 2
cdplib/db_handlers/MongodbHandler.py

@@ -134,7 +134,7 @@ class MongodbHandler:
             "Parameter 'direction' must be a string type"
             
         self._database[collection_name].create_index([(key, direction)], name=key)
-        #collection.create_index([('field_i_want_to_index', pymongo.TEXT)], name='search_index', default_language='english')
+        
 
     def set_collection_schema(self, collection_name: str, schema_path: str,
                               validation_level: str = 'moderate',validation_action: str = 'error'):
@@ -257,7 +257,7 @@ class MongodbHandler:
                 data = self._database[collection_name].find({attribute: {comparison_operator: attribute_value}})
 
         except Exception as error:
-            self._log.log_and_raise_error(('An error occured trying to query data from {}, with query {}: {}:{}. \nError:{}').format(collection_name, attribute_value, comparison_operator, attribute_value, error))
+            self._log.log_and_raise_error(('An error occured trying to query data from {}, with query {}: {}:{}. \nError:{}').format(collection_name, attribute, comparison_operator, attribute_value, error))
 
         return self.convert_mongo_data_into_dataframe(data, index, collection_name)