Thanks to visit codestin.com
Credit goes to github.com

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

Parse column names in a dataframe to avoid breaking the line protocol #584

Merged
merged 12 commits into from
May 8, 2018
Merged
2 changes: 2 additions & 0 deletions influxdb/_dataframe_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ def _convert_dataframe_to_lines(self,
raise TypeError('Must be DataFrame with DatetimeIndex or '
'PeriodIndex.')

dataframe = dataframe.rename(
columns={item: _escape_tag(item) for item in dataframe.columns})
# Create a Series of columns for easier indexing
column_series = pd.Series(dataframe.columns)

Expand Down
20 changes: 20 additions & 0 deletions influxdb/tests/dataframe_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ def test_dataframe_write_points_with_whitespace_measurement(self):
cli.write_points(dataframe, 'meas with space')
self.assertEqual(m.last_request.body, expected)

def test_dataframe_write_points_with_whitespace_in_column_names(self):
"""write_points should escape white space in column names."""
now = pd.Timestamp('1970-01-01 00:00+00:00')
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]],
index=[now, now + timedelta(hours=1)],
columns=["column one", "column two",
"column three"])
expected = (
b"foo column\\ one=\"1\",column\\ two=1i,column\\ three=1.0 0\n"
b"foo column\\ one=\"2\",column\\ two=2i,column\\ three=2.0 "
b"3600000000000\n"
)
with requests_mock.Mocker() as m:
m.register_uri(requests_mock.POST,
"http://localhost:8086/write",
status_code=204)
cli = DataFrameClient(database='db')
cli.write_points(dataframe, 'foo')
self.assertEqual(m.last_request.body, expected)

def test_write_points_from_dataframe_with_none(self):
"""Test write points from df in TestDataFrameClient object."""
now = pd.Timestamp('1970-01-01 00:00+00:00')
Expand Down