Browse Source

added insert_dataframe method to influx handler

tanja 3 years ago
parent
commit
4c6223c959
1 changed files with 23 additions and 0 deletions
  1. 23 0
      cdplib/db_handlers/InfluxdbHandler.py

+ 23 - 0
cdplib/db_handlers/InfluxdbHandler.py

@@ -115,3 +115,26 @@ class InfluxdbHandler:
                 '\' tz(\'Europe/Berlin\');'
 
         return self.query_to_dataframe(query)
+
+    def insert_dataframe(self, dataframe: pd.DataFrame,
+                         batch_size: int = 10000,
+                         time_precision: str = 'u'):
+        """
+        :param dataframe: DESCRIPTION
+        :type dataframe: pd.DataFrame
+        :return: DESCRIPTION
+        :rtype: TYPE
+
+        """
+        for column in dataframe.columns:
+            try:
+                self.client.write_points(
+                    dataframe=dataframe[column].to_frame(),
+                    measurement=column,
+                    protocol='line',
+                    batch_size=batch_size,
+                    time_precision=time_precision)
+
+            except Exception as error:
+                self._logger.loger_and_raise_error(
+                    ('Could not insert data, Error: {}'.format(error)))