diff --git a/.gitignore b/.gitignore index 7720b658..49d681f6 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ docs/build/ .coverage cover env +.vscode \ No newline at end of file diff --git a/influxdb/_dataframe_client.py b/influxdb/_dataframe_client.py index d16e29ca..2db0cc10 100644 --- a/influxdb/_dataframe_client.py +++ b/influxdb/_dataframe_client.py @@ -72,6 +72,11 @@ def write_points(self, datatypes. Defaults to None, which preserves 14-15 significant figures for float and all significant figures for int datatypes. """ + # Check measurement fields types in database and convert if it is possible + measurement_type = self._get_measurement_type(measurement) + # Convert data to measurement type in database + dataframe = dataframe.astype(measurement_type) + if tag_columns is None: tag_columns = [] @@ -467,3 +472,31 @@ def _datetime_to_epoch(self, datetime, time_precision='s'): return seconds * 1e6 elif time_precision == 'n': return seconds * 1e9 + + def _get_measurement_type(self, measurement, database=None): + """ Get measurement type from database measurements if exists. Else, None. """ + query = 'SHOW FIELD KEYS ON "%s" FROM "%s"' % (database, measurement) + + field_list = list(self.query(query)) + + if len(field_list) > 0: + types_dict = {} + for row in field_list[0]: + # Convert string data type into type + if row['fieldType'] == 'float': + datatype = float + elif row['fieldType'] == 'integer': + datatype = int + elif row['fieldType'] == 'boolean': + datatype = bool + elif row['fieldType'] == 'string': + datatype = str + elif row['fieldType'] == 'timestamp': + datatype = pd.Timestamp + else: + raise KeyError('InfluxDB type "%s" is unknown.' % row['fieldType']) + + types_dict[row['fieldKey']] = datatype + return types_dict + else: + return None \ No newline at end of file