Skip to content

Commit 08e0299

Browse files
d3banjanaviau
authored andcommitted
Line protocol leading comma (influxdata#694)
* [fix] typo in comment + [fix] handles leading comma for the case that the first value column is Null valued * [refactor] consolidated similar logic to a new function * [fix] covering scenario where is a string * Revert "[fix] covering scenario where is a string" This reverts commit 49af5ab. * Revert "[refactor] consolidated similar logic to a new function" This reverts commit 366e771. * [tests][feature] added tests to check if first none value results in invalid line protocol * [fix] deleted debug lines * [fix] overspecified date_range args * [fix] overspecified date_range args * [fix] removed endpoint in date-range * [fix] reordered columns in test target * [fix] [test] freeze order of columns * [refactor] [test] used loc instead of dict-like invocation of columns * [fix] [test] [lint] cleared up complainsts from flake8 and pep257
1 parent d5d1249 commit 08e0299

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

influxdb/_dataframe_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def _convert_dataframe_to_lines(self,
363363
tag_df = self._stringify_dataframe(
364364
tag_df, numeric_precision, datatype='tag')
365365

366-
# join preprendded tags, leaving None values out
366+
# join prepended tags, leaving None values out
367367
tags = tag_df.apply(
368368
lambda s: [',' + s.name + '=' + v if v else '' for v in s])
369369
tags = tags.sum(axis=1)
@@ -392,6 +392,8 @@ def _convert_dataframe_to_lines(self,
392392
field_df.columns[1:]]
393393
field_df = field_df.where(~mask_null, '') # drop Null entries
394394
fields = field_df.sum(axis=1)
395+
# take out leading , where first column has a Null value
396+
fields = fields.str.lstrip(",")
395397
del field_df
396398

397399
# Generate line protocol string

influxdb/tests/dataframe_client_test.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,71 @@ def test_write_points_from_dataframe_with_numeric_column_names(self):
389389

390390
self.assertEqual(m.last_request.body, expected)
391391

392+
def test_write_points_from_dataframe_with_leading_none_column(self):
393+
"""write_points detect erroneous leading comma for null first field."""
394+
dataframe = pd.DataFrame(
395+
dict(
396+
first=[1, None, None, 8, 9],
397+
second=[2, None, None, None, 10],
398+
third=[3, 4.1, None, None, 11],
399+
first_tag=["one", None, None, "eight", None],
400+
second_tag=["two", None, None, None, None],
401+
third_tag=["three", "four", None, None, None],
402+
comment=[
403+
"All columns filled",
404+
"First two of three empty",
405+
"All empty",
406+
"Last two of three empty",
407+
"Empty tags with values",
408+
]
409+
),
410+
index=pd.date_range(
411+
start=pd.to_datetime('2018-01-01'),
412+
freq='1D',
413+
periods=5,
414+
)
415+
)
416+
expected = (
417+
b'foo,first_tag=one,second_tag=two,third_tag=three'
418+
b' comment="All columns filled",first=1.0,second=2.0,third=3.0'
419+
b' 1514764800000000000\n'
420+
b'foo,third_tag=four'
421+
b' comment="First two of three empty",third=4.1'
422+
b' 1514851200000000000\n'
423+
b'foo comment="All empty" 1514937600000000000\n'
424+
b'foo,first_tag=eight'
425+
b' comment="Last two of three empty",first=8.0'
426+
b' 1515024000000000000\n'
427+
b'foo'
428+
b' comment="Empty tags with values",first=9.0,second=10.0'
429+
b',third=11.0'
430+
b' 1515110400000000000\n'
431+
)
432+
433+
with requests_mock.Mocker() as m:
434+
m.register_uri(requests_mock.POST,
435+
"http://localhost:8086/write",
436+
status_code=204)
437+
438+
cli = DataFrameClient(database='db')
439+
440+
colnames = [
441+
"first_tag",
442+
"second_tag",
443+
"third_tag",
444+
"comment",
445+
"first",
446+
"second",
447+
"third"
448+
]
449+
cli.write_points(dataframe.loc[:, colnames], 'foo',
450+
tag_columns=[
451+
"first_tag",
452+
"second_tag",
453+
"third_tag"])
454+
455+
self.assertEqual(m.last_request.body, expected)
456+
392457
def test_write_points_from_dataframe_with_numeric_precision(self):
393458
"""Test write points from df with numeric precision."""
394459
now = pd.Timestamp('1970-01-01 00:00+00:00')

0 commit comments

Comments
 (0)