Skip to content

Commit ffae906

Browse files
authored
fix: skip DataFrame rows without data (#170)
1 parent c8fd3b5 commit ffae906

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## 1.13.0 [unreleased]
22

3+
### Bug Fixes
4+
1. [#164](https://github.com/influxdata/influxdb-client-python/pull/170): Skip DataFrame rows without data - all fields are nan.
5+
6+
37
## 1.12.0 [2020-10-30]
48

59
1. [#163](https://github.com/influxdata/influxdb-client-python/pull/163): Added support for Python 3.9

influxdb_client/client/write/dataframe_serializer.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ def _itertuples(data_frame):
3737
return zip(data_frame.index, *cols)
3838

3939

40+
def _is_nan(x):
41+
return x != x
42+
43+
44+
def _any_not_nan(p, indexes):
45+
return any(map(lambda inx: not _is_nan(p[inx]), indexes))
46+
47+
4048
def data_frame_to_list_of_points(data_frame, point_settings, **kwargs):
4149
"""Serialize DataFrame into LineProtocols."""
4250
from ...extras import pd, np
@@ -61,6 +69,7 @@ def data_frame_to_list_of_points(data_frame, point_settings, **kwargs):
6169

6270
tags = []
6371
fields = []
72+
fields_indexes = []
6473
keys = []
6574

6675
if point_settings.defaultTags:
@@ -73,14 +82,18 @@ def data_frame_to_list_of_points(data_frame, point_settings, **kwargs):
7382
keys.append(key.translate(_ESCAPE_KEY))
7483
key_format = f'{{keys[{index}]}}'
7584

85+
index_value = index + 1
7686
if key in data_frame_tag_columns:
77-
tags.append({'key': key, 'value': f"{key_format}={{str(p[{index + 1}]).translate(_ESCAPE_KEY)}}"})
87+
tags.append({'key': key, 'value': f"{key_format}={{str(p[{index_value}]).translate(_ESCAPE_KEY)}}"})
7888
elif issubclass(value.type, np.integer):
79-
fields.append(f"{key_format}={{p[{index + 1}]}}i")
89+
fields.append(f"{key_format}={{p[{index_value}]}}i")
90+
fields_indexes.append(index_value)
8091
elif issubclass(value.type, (np.float, np.bool_)):
81-
fields.append(f"{key_format}={{p[{index + 1}]}}")
92+
fields.append(f"{key_format}={{p[{index_value}]}}")
93+
fields_indexes.append(index_value)
8294
else:
83-
fields.append(f"{key_format}=\"{{str(p[{index + 1}]).translate(_ESCAPE_STRING)}}\"")
95+
fields.append(f"{key_format}=\"{{str(p[{index_value}]).translate(_ESCAPE_STRING)}}\"")
96+
fields_indexes.append(index_value)
8497

8598
tags.sort(key=lambda x: x['key'])
8699
tags = ','.join(map(lambda y: y['value'], tags))
@@ -100,7 +113,7 @@ def data_frame_to_list_of_points(data_frame, point_settings, **kwargs):
100113
if isnull.any():
101114
rep = _replace(data_frame)
102115
lp = (reduce(lambda a, b: re.sub(*b, a), rep, f(p))
103-
for p in _itertuples(data_frame))
116+
for p in filter(lambda x: _any_not_nan(x, fields_indexes), _itertuples(data_frame)))
104117
return list(lp)
105118
else:
106119
return list(map(f, _itertuples(data_frame)))

tests/test_WriteApiDataFrame.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ def test_write_nan(self):
9999
data_frame = pd.DataFrame(data=[[3.1955, np.nan, 20.514305, np.nan],
100100
[5.7310, np.nan, 23.328710, np.nan],
101101
[np.nan, 3.138664, np.nan, 20.755026],
102-
[5.7310, 5.139563, 23.328710, 19.791240]],
102+
[5.7310, 5.139563, 23.328710, 19.791240],
103+
[np.nan, np.nan, np.nan, np.nan]],
103104
index=[now, now + timedelta(minutes=30), now + timedelta(minutes=60),
104-
now + timedelta(minutes=90)],
105+
now + timedelta(minutes=90), now + timedelta(minutes=120)],
105106
columns=["actual_kw_price", "forecast_kw_price", "actual_general_use",
106107
"forecast_general_use"])
107108

0 commit comments

Comments
 (0)