Skip to content

Commit ed4561b

Browse files
dmuirurixginn8
authored andcommitted
Parse column names in a dataframe to avoid breaking the line protocol (influxdata#584)
* Remove comment as issues have been resolved * Parse column names in a dataframe to handle spaces in tag of field keys * Patch for ERROR: Test failed write points from df with series * Patch for flake8 issues(E231, E501) * Test case for spaces in column names * flake8 issues * flake8 issues: trailing wspace * Testing if test case would catch a regression * Test catches a regressed build * Re-run the build
1 parent fd4579c commit ed4561b

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

influxdb/_dataframe_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ def _convert_dataframe_to_lines(self,
288288
raise TypeError('Must be DataFrame with DatetimeIndex or '
289289
'PeriodIndex.')
290290

291+
dataframe = dataframe.rename(
292+
columns={item: _escape_tag(item) for item in dataframe.columns})
291293
# Create a Series of columns for easier indexing
292294
column_series = pd.Series(dataframe.columns)
293295

influxdb/tests/dataframe_client_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,26 @@ def test_dataframe_write_points_with_whitespace_measurement(self):
8181
cli.write_points(dataframe, 'meas with space')
8282
self.assertEqual(m.last_request.body, expected)
8383

84+
def test_dataframe_write_points_with_whitespace_in_column_names(self):
85+
"""write_points should escape white space in column names."""
86+
now = pd.Timestamp('1970-01-01 00:00+00:00')
87+
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]],
88+
index=[now, now + timedelta(hours=1)],
89+
columns=["column one", "column two",
90+
"column three"])
91+
expected = (
92+
b"foo column\\ one=\"1\",column\\ two=1i,column\\ three=1.0 0\n"
93+
b"foo column\\ one=\"2\",column\\ two=2i,column\\ three=2.0 "
94+
b"3600000000000\n"
95+
)
96+
with requests_mock.Mocker() as m:
97+
m.register_uri(requests_mock.POST,
98+
"http://localhost:8086/write",
99+
status_code=204)
100+
cli = DataFrameClient(database='db')
101+
cli.write_points(dataframe, 'foo')
102+
self.assertEqual(m.last_request.body, expected)
103+
84104
def test_write_points_from_dataframe_with_none(self):
85105
"""Test write points from df in TestDataFrameClient object."""
86106
now = pd.Timestamp('1970-01-01 00:00+00:00')

0 commit comments

Comments
 (0)