@@ -358,17 +358,11 @@ def _convert_dataframe_to_lines(self,
358
358
tag_columns .append (tag )
359
359
360
360
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
372
366
elif global_tags :
373
367
tag_string = '' .join (
374
368
[",{}={}" .format (k , _escape_tag (v )) if v else ''
@@ -382,22 +376,16 @@ def _convert_dataframe_to_lines(self,
382
376
field_df = dataframe [field_columns ]
383
377
# Keep the positions where Null values are found
384
378
mask_null = field_df .isnull ().values
385
-
386
379
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
398
384
399
385
# Generate line protocol string
400
386
measurement = _escape_tag (measurement )
387
+ # prepend comma to non-Null tag-rows
388
+ tags = ("," + tags ).str .replace (r"^,$" ,"" )
401
389
points = (measurement + tags + ' ' + fields + ' ' + time ).tolist ()
402
390
return points
403
391
@@ -453,6 +441,22 @@ def _stringify_dataframe(dframe, numeric_precision, datatype='field'):
453
441
454
442
return dframe
455
443
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
+
456
460
def _datetime_to_epoch (self , datetime , time_precision = 's' ):
457
461
seconds = (datetime - self .EPOCH ).total_seconds ()
458
462
if time_precision == 'h' :
0 commit comments