8000 fix: serialization to DataFrame with nan values (#378) · Pragatibs/influxdb-client-python@0ebe302 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ebe302

Browse files
authored
fix: serialization to DataFrame with nan values (influxdata#378)
1 parent 7e09dde commit 0ebe302

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Bug Fixes
44
1. [#375](https://github.com/influxdata/influxdb-client-python/pull/375): Construct `InfluxDBError` without HTTP response
5+
1. [#378](https://github.com/influxdata/influxdb-client-python/pull/378): Correct serialization DataFrame with nan values [DataFrame]
56

67
### CI
78
1. [#370](https://github.com/influxdata/influxdb-client-python/pull/370): Add Python 3.10 to CI builds

influxdb_client/client/write/dataframe_serializer.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ def _any_not_nan(p, indexes):
2727
return any(map(lambda x: _not_nan(p[x]), indexes))
2828

2929

30-
_EMPTY_EXPRESSION = "_EMPTY_LINE_PROTOCOL_PART_"
31-
32-
3330
class DataframeSerializer:
3431
"""Serialize DataFrame into LineProtocols."""
3532

@@ -180,15 +177,13 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
180177
field_value = f'{sep}{key_format}={{{val_format}}}'
181178
elif issubclass(value.type, np.floating):
182179
if null_columns[index]:
183-
field_value = f"""{{
184-
"{sep}{_EMPTY_EXPRESSION}" if math.isnan({val_format}) else f"{sep}{key_format}={{{val_format}}}"
185-
}}"""
180+
field_value = f"""{{"" if math.isnan({val_format}) else f"{sep}{key_format}={{{val_format}}}"}}"""
186181
else:
187182
field_value = f'{sep}{key_format}={{{val_format}}}'
188183
else:
189184
if null_columns[index]:
190185
field_value = f"""{{
191-
'{sep}{_EMPTY_EXPRESSION}' if type({val_format}) == float and math.isnan({val_format}) else
186+
'' if type({val_format}) == float and math.isnan({val_format}) else
192187
f'{sep}{key_format}="{{str({val_format}).translate(_ESCAPE_STRING)}}"'
193188
}}"""
194189
else:
@@ -249,7 +244,7 @@ def serialize(self, chunk_idx: int = None):
249244
if self.first_field_maybe_null:
250245
# When the first field is null (None/NaN), we'll have
251246
# a spurious leading comma which needs to be removed.
252-
lp = (re.sub(f",{_EMPTY_EXPRESSION}|{_EMPTY_EXPRESSION},|{_EMPTY_EXPRESSION}", '', self.f(p))
247+
lp = (re.sub('^(( |[^ ])* ),([a-zA-Z])(.*)', '\\1\\3\\4', self.f(p))
253248
for p in filter(lambda x: _any_not_nan(x, self.field_indexes), _itertuples(chunk)))
254249
return list(lp)
255250
else:

tests/test_WriteApiDataFrame.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,23 @@ def test_serialize_strings_with_commas(self):
396396
self.assertEqual("bookings,Account=Testaccount,Category=Testcategory,Entry\\ Type=Expense Currencs=\"EUR\",Note=\"This, works\",Recurring=\"no\",Value=-1.0 1538352000000000000", points[0])
397397
self.assertEqual("bookings,Account=Testaccount,Category=Testcategory,Entry\\ Type=Expense Currencs=\"EUR\",Note=\"This , works not\",Recurring=\"no\",Value=-1.0 1538438400000000000", points[1])
398398

399+
def test_without_tags_and_fields_with_nan(self):
400+
from influxdb_client.extras import pd, np
401+
402+
df = pd.DataFrame({
403+
'a': np.arange(0., 3.),
404+
'b': [0., np.nan, 1.],
405+
}).set_index(pd.to_datetime(['2021-01-01 0:00', '2021-01-01 0:01', '2021-01-01 0:02']))
406+
407+
points = data_frame_to_list_of_points(data_frame=df,
408+
data_frame_measurement_name="test",
409+
point_settings=PointSettings())
410+
411+
self.assertEqual(3, len(points))
412+
self.assertEqual("test a=0.0,b=0.0 1609459200000000000", points[0])
413+
self.assertEqual("test a=1.0 1609459260000000000", points[1])
414+
self.assertEqual("test a=2.0,b=1.0 1609459320000000000", points[2])
415+
399416

400417
class DataSerializerChunksTest(unittest.TestCase):
401418
def test_chunks(self):

0 commit comments

Comments
 (0)
0