Skip to content

Commit 56062c5

Browse files
committed
Minor changes to _stringify_dataframe. Added test for numeric precision.
1 parent 423b8c9 commit 56062c5

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

influxdb/_dataframe_client.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def _stringify_dataframe(self,
305305
datatype='field'):
306306

307307
# Find int and string columns for field-type data
308-
int_columns = dataframe.select_dtypes(include=['int']).columns
308+
int_columns = dataframe.select_dtypes(include=['integer']).columns
309309
string_columns = dataframe.select_dtypes(include=['object']).columns
310310

311311
# Convert dataframe to string
@@ -323,11 +323,20 @@ def _stringify_dataframe(self,
323323
.astype(str))
324324
elif isinstance(numeric_precision, int):
325325
# If precision is specified, round to appropriate precision
326-
numeric_columns = (dataframe.select_dtypes(include=['number'])
327-
.columns)
328-
dataframe[numeric_columns] = (dataframe[numeric_columns]
329-
.round(numeric_precision))
330-
dataframe = dataframe.astype(str)
326+
float_columns = (dataframe.select_dtypes(include=['floating'])
327+
.columns)
328+
nonfloat_columns = dataframe.columns[~dataframe.columns.isin(
329+
float_columns)]
330+
dataframe[float_columns] = (dataframe[float_columns]
331+
.round(numeric_precision))
332+
# If desired precision is > 10 decimal places, need to use repr
333+
if numeric_precision > 10:
334+
dataframe[float_columns] = (dataframe[float_columns]
335+
.applymap(repr))
336+
dataframe[nonfloat_columns] = (dataframe[nonfloat_columns]
337+
.astype(str))
338+
else:
339+
dataframe = dataframe.astype(str)
331340
else:
332341
raise ValueError('Invalid numeric precision')
333342

influxdb/tests/dataframe_client_test.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def test_write_points_from_dataframe_in_batches(self):
6969
cli = DataFrameClient(database='db')
7070
self.assertTrue(cli.write_points(dataframe, "foo", batch_size=1))
7171

72-
def test_write_points_with_tag_columns(self):
72+
def test_write_points_from_dataframe_with_tag_columns(self):
7373
now = pd.Timestamp('1970-01-01 00:00+00:00')
7474
dataframe = pd.DataFrame(data=[['blue', 1, "1", 1, 1.0],
7575
['red', 0, "2", 2, 2.0]],
@@ -100,7 +100,7 @@ def test_write_points_with_tag_columns(self):
100100
tag_columns=['tag_one', 'tag_two'], tags=None)
101101
self.assertEqual(m.last_request.body, expected)
102102

103-
def test_write_points_with_tag_columns_and_global_tags(self):
103+
def test_write_points_from_dataframe_with_tag_cols_and_global_tags(self):
104104
now = pd.Timestamp('1970-01-01 00:00+00:00')
105105
dataframe = pd.DataFrame(data=[['blue', 1, "1", 1, 1.0],
106106
['red', 0, "2", 2, 2.0]],
@@ -128,7 +128,7 @@ def test_write_points_with_tag_columns_and_global_tags(self):
128128
tags={'global_tag': 'value'})
129129
self.assertEqual(m.last_request.body, expected)
130130

131-
def test_write_points_with_tag_columns_and_defaults(self):
131+
def test_write_points_from_dataframe_with_tag_cols_and_defaults(self):
132132
now = pd.Timestamp('1970-01-01 00:00+00:00')
133133
dataframe = pd.DataFrame(data=[['blue', 1, "1", 1, 1.0, 'hot'],
134134
['red', 0, "2", 2, 2.0, 'cold']],
@@ -219,6 +219,50 @@ def test_write_points_from_dataframe_with_numeric_column_names(self):
219219

220220
self.assertEqual(m.last_request.body, expected)
221221

222+
def test_write_points_from_dataframe_with_numeric_precision(self):
223+
now = pd.Timestamp('1970-01-01 00:00+00:00')
224+
# df with numeric column names
225+
dataframe = pd.DataFrame(data=[["1", 1, 1.1111111111111],
226+
["2", 2, 2.2222222222222]],
227+
index=[now, now + timedelta(hours=1)])
228+
229+
expected_default_precision = (
230+
b'foo,hello=there 0=\"1\",1=1i,2=1.11111111111 0\n'
231+
b'foo,hello=there 0=\"2\",1=2i,2=2.22222222222 3600000000000\n'
232+
)
233+
234+
expected_specified_precision = (
235+
b'foo,hello=there 0=\"1\",1=1i,2=1.1111 0\n'
236+
b'foo,hello=there 0=\"2\",1=2i,2=2.2222 3600000000000\n'
237+
)
238+
239+
expected_full_precision = (
240+
b'foo,hello=there 0=\"1\",1=1i,2=1.1111111111111 0\n'
241+
b'foo,hello=there 0=\"2\",1=2i,2=2.2222222222222 3600000000000\n'
242+
)
243+
244+
with requests_mock.Mocker() as m:
245+
m.register_uri(requests_mock.POST,
246+
"http://localhost:8086/write",
247+
status_code=204)
248+
249+
cli = DataFrameClient(database='db')
250+
cli.write_points(dataframe, "foo", {"hello": "there"})
251+
252+
self.assertEqual(m.last_request.body, expected_default_precision)
253+
254+
cli = DataFrameClient(database='db')
255+
cli.write_points(dataframe, "foo", {"hello": "there"},
256+
numeric_precision=4)
257+
258+
self.assertEqual(m.last_request.body, expected_specified_precision)
259+
260+
cli = DataFrameClient(database='db')
261+
cli.write_points(dataframe, "foo", {"hello": "there"},
262+
numeric_precision='full')
263+
264+
self.assertEqual(m.last_request.body, expected_full_precision)
265+
222266
def test_write_points_from_dataframe_with_period_index(self):
223267
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]],
224268
index=[pd.Period('1970-01-01'),

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy