Skip to content

Commit a421a6f

Browse files
authored
fix: serialize numpy floats (#254)
1 parent bccc2b8 commit a421a6f

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
1. [#241](https://github.com/influxdata/influxdb-client-python/pull/241): Add detail error message for not supported type of `Point.field`
66
1. [#238](https://github.com/influxdata/influxdb-client-python/pull/238): Add possibility to specify default `timezone` for datetimes without `tzinfo`
77

8+
### Bug Fixes
9+
1. [#254](https://github.com/influxdata/influxdb-client-python/pull/254): Serialize `numpy` floats into LineProtocol
10+
811
### Documentation
912
1. [#255](https://github.com/influxdata/influxdb-client-python/pull/255): Fix invalid description for env var `INFLUXDB_V2_CONNECTION_POOL_MAXSIZE`
1013

influxdb_client/client/write/dataframe_serializer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def data_frame_to_list_of_points(data_frame, point_settings, **kwargs):
158158
field_value = f"{sep}{key_format}={{{val_format}}}i"
159159
elif issubclass(value.type, np.bool_):
160160
field_value = f'{sep}{key_format}={{{val_format}}}'
161-
elif issubclass(value.type, np.float):
161+
elif issubclass(value.type, np.floating):
162162
if null_columns[index]:
163163
field_value = f"""{{"" if math.isnan({val_format}) else f"{sep}{key_format}={{{val_format}}}"}}"""
164164
else:

tests/test_WriteApiDataFrame.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import csv
2-
import os
31
import time
42
import unittest
53
from datetime import timedelta
@@ -160,7 +158,7 @@ def test_write_object_field_nan(self):
160158
points[1])
161159

162160
def test_write_field_bool(self):
163-
from influxdb_client.extras import pd, np
161+
from influxdb_client.extras import pd
164162

165163
now = pd.Timestamp('2020-04-05 00:00+00:00')
166164

@@ -252,7 +250,7 @@ def test_tags_order(self):
252250
self.assertEqual("h2o,a=a,b=b,c=c level=2i 1586048400000000000", points[0])
253251

254252
def test_escape_text_value(self):
255-
from influxdb_client.extras import pd, np
253+
from influxdb_client.extras import pd
256254

257255
now = pd.Timestamp('2020-04-05 00:00+00:00')
258256
an_hour_ago = now - timedelta(hours=1)
@@ -273,7 +271,7 @@ def test_escape_text_value(self):
273271
self.assertEqual("test,d=bar\\ foo b=\"goodbye cruel world\",c=2i 1586044800000000000", points[1])
274272

275273
def test_with_default_tags(self):
276-
from influxdb_client.extras import pd, np
274+
from influxdb_client.extras import pd
277275
now = pd.Timestamp('2020-04-05 00:00+00:00')
278276
data_frame = pd.DataFrame(data={
279277
'value': [1, 2],
@@ -309,8 +307,7 @@ def test_with_default_tags(self):
309307
self.assertEqual(True, (data_frame == original_data).all(axis = None), f'data changed; old:\n{original_data}\nnew:\n{data_frame}')
310308

311309
def test_with_period_index(self):
312-
from influxdb_client.extras import pd, np
313-
now = pd.Timestamp('2020-04-05 00:00+00:00')
310+
from influxdb_client.extras import pd
314311
data_frame = pd.DataFrame(data={
315312
'value': [1, 2],
316313
},
@@ -323,3 +320,16 @@ def test_with_period_index(self):
323320
self.assertEqual(2, len(points))
324321
self.assertEqual("h2o value=1i 1586048400000000000", points[0])
325322
self.assertEqual("h2o value=2i 1586052000000000000", points[1])
323+
324+
def test_write_num_py_floats(self):
325+
from influxdb_client.extras import pd, np
326+
now = pd.Timestamp('2020-04-05 00:00+00:00')
327+
328+
for np_float_type in [np.float, np.float16, np.float32, np.float64, np.float128]:
329+
data_frame = pd.DataFrame([15.5], index=[now], columns=['level']).astype(np_float_type)
330+
points = data_frame_to_list_of_points(data_frame=data_frame,
331+
data_frame_measurement_name='h2o',
332+
point_settings=PointSettings())
333+
self.assertEqual(1, len(points))
334+
self.assertEqual("h2o level=15.5 1586044800000000000", points[0], msg=f'Current type: {np_float_type}')
335+

0 commit comments

Comments
 (0)