Skip to content

Commit 366e771

Browse files
author
Debanjan
committed
[refactor] consolidated similar logic to a new function
1 parent b4ceab9 commit 366e771

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

influxdb/_dataframe_client.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -358,17 +358,11 @@ def _convert_dataframe_to_lines(self,
358358
tag_columns.append(tag)
359359

360360
tag_df = dataframe[tag_columns]
361-
tag_df = tag_df.fillna('') # replace NA with empty string
362-
tag_df = tag_df.sort_index(axis=1)
363-
tag_df = self._stringify_dataframe(
364-
tag_df, numeric_precision, datatype='tag')
365-
366-
# join prepended tags, leaving None values out
367-
tags = tag_df.apply(
368-
lambda s: [',' + s.name + '=' + v if v else '' for v in s])
369-
tags = tags.sum(axis=1)
370-
371-
del tag_df
361+
# Keep the positions where Null values are found
362+
mask_null = tag_df.isnull().values
363+
tag_df = self._stringify_dataframe(tag_df, numeric_precision, datatype='tag')
364+
tags = self._lineify_string_df(tag_df,mask_null)
365+
del tag_df, mask_null
372366
elif global_tags:
373367
tag_string = ''.join(
374368
[",{}={}".format(k, _escape_tag(v)) if v else ''
@@ -382,22 +376,16 @@ def _convert_dataframe_to_lines(self,
382376
field_df = dataframe[field_columns]
383377
# Keep the positions where Null values are found
384378
mask_null = field_df.isnull().values
385-
386379
field_df = self._stringify_dataframe(field_df,
387-
numeric_precision,
388-
datatype='field')
389-
390-
field_df = (field_df.columns.values + '=').tolist() + field_df
391-
field_df[field_df.columns[1:]] = ',' + field_df[
392-
field_df.columns[1:]]
393-
field_df = field_df.where(~mask_null, '') # drop Null entries
394-
fields = field_df.sum(axis=1)
395-
# take out leading , where first column has a Null value
396-
fields = fields.str.lstrip(",")
397-
del field_df
380+
numeric_precision,
381+
datatype='field')
382+
fields = self._lineify_string_df(field_df, mask_null)
383+
del field_df, mask_null
398384

399385
# Generate line protocol string
400386
measurement = _escape_tag(measurement)
387+
# prepend comma to non-Null tag-rows
388+
tags = ("," + tags).str.replace(r"^,$","")
401389
points = (measurement + tags + ' ' + fields + ' ' + time).tolist()
402390
return points
403391

@@ -453,6 +441,22 @@ def _stringify_dataframe(dframe, numeric_precision, datatype='field'):
453441

454442
return dframe
455443

444+
445+
def _lineify_string_df(self,string_df,mask_null):
446+
"""accepts a dataframe of tag or field df
447+
returns a Series of strings joined by
448+
comma if non-Null values using
449+
vector string operations"""
450+
df = string_df.copy()
451+
df = (df.columns.values + '=').tolist() + df
452+
df[df.columns[1:]] = ',' + df[df.columns[1:]]
453+
df = df.where(~mask_null, '') # drop Null entries
454+
lineified_string_series = df.sum(axis=1)
455+
# take out leading comma where first column has a Null value
456+
lineified_string_series = lineified_string_series.str.lstrip(",")
457+
return lineified_string_series
458+
459+
456460
def _datetime_to_epoch(self, datetime, time_precision='s'):
457461
seconds = (datetime - self.EPOCH).total_seconds()
458462
if time_precision == 'h':

0 commit comments

Comments
 (0)