Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Types matching #741

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ docs/build/
.coverage
cover
env
.vscode
Copy link
Collaborator

@aviau aviau Jul 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in your system's global .gitignore.

It does not make sense to add every single file created by every single editor in existence to every single project's gitignore.

Look at this article for help on ignoring files with git.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okey, thanks for link!

33 changes: 33 additions & 0 deletions influxdb/_dataframe_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down Expand Up @@ -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