@@ -19,14 +19,6 @@ def _itertuples(data_frame):
19
19
return zip (data_frame .index , * cols )
20
20
21
21
22
- def _not_nan (x ):
23
- return x == x
24
-
25
-
26
- def _any_not_nan (p , indexes ):
27
- return any (map (lambda x : _not_nan (p [x ]), indexes ))
28
-
29
-
30
22
class DataframeSerializer :
31
23
"""Serialize DataFrame into LineProtocols."""
32
24
@@ -77,7 +69,7 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
77
69
# When NaNs are present, the expression looks like this (split
78
70
# across two lines to satisfy the code-style checker)
79
71
#
80
- # lambda p: f"""{measurement_name} {"" if math.isnan (p[1])
72
+ # lambda p: f"""{measurement_name} {"" if pd.isna (p[1])
81
73
# else f"{keys[0]}={p[1]}"},{keys[1]}={p[2]}i {p[0].value}"""
82
74
#
83
75
# When there's a NaN value in column a, we'll end up with a comma at the start of the
@@ -175,7 +167,7 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
175
167
# This column is a tag column.
176
168
if null_columns .iloc [index ]:
177
169
key_value = f"""{{
178
- '' if { val_format } == '' or type( { val_format } ) == float and math.isnan ({ val_format } ) else
170
+ '' if { val_format } == '' or pd.isna ({ val_format } ) else
179
171
f',{ key_format } ={{str({ val_format } ).translate(_ESCAPE_STRING)}}'
180
172
}}"""
181
173
else :
@@ -192,19 +184,16 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
192
184
# field column has no nulls, we don't run the comma-removal
193
185
# regexp substitution step.
194
186
sep = '' if len (field_indexes ) == 0 else ','
195
- if issubclass (value .type , np .integer ):
196
- field_value = f"{ sep } { key_format } ={{{ val_format } }}i"
197
- elif issubclass (value .type , np .bool_ ):
198
- field_value = f'{ sep } { key_format } ={{{ val_format } }}'
199
- elif issubclass (value .type , np .floating ):
187
+ if issubclass (value .type , np .integer ) or issubclass (value .type , np .floating ) or issubclass (value .type , np .bool_ ): # noqa: E501
188
+ suffix = 'i' if issubclass (value .type , np .integer ) else ''
200
189
if null_columns .iloc [index ]:
201
- field_value = f"""{{"" if math.isnan ({ val_format } ) else f"{ sep } { key_format } ={{{ val_format } }}"}}"""
190
+ field_value = f"""{{"" if pd.isna ({ val_format } ) else f"{ sep } { key_format } ={{{ val_format } }}{ suffix } "}}""" # noqa: E501
202
191
else :
203
- field_value = f' { sep } { key_format } ={{{ val_format } }}'
192
+ field_value = f" { sep } { key_format } ={{{ val_format } }}{ suffix } "
204
193
else :
205
194
if null_columns .iloc [index ]:
206
195
field_value = f"""{{
207
- '' if type( { val_format } ) == float and math.isnan ({ val_format } ) else
196
+ '' if pd.isna ({ val_format } ) else
208
197
f'{ sep } { key_format } ="{{str({ val_format } ).translate(_ESCAPE_STRING)}}"'
209
198
}}"""
210
199
else :
@@ -229,17 +218,21 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
229
218
'_ESCAPE_KEY' : _ESCAPE_KEY ,
230
219
'_ESCAPE_STRING' : _ESCAPE_STRING ,
231
220
'keys' : keys ,
232
- 'math ' : math ,
221
+ 'pd ' : pd ,
233
222
})
234
223
235
224
for k , v in dict (data_frame .dtypes ).items ():
236
225
if k in data_frame_tag_columns :
237
226
data_frame = data_frame .replace ({k : '' }, np .nan )
238
227
228
+ def _any_not_nan (p , indexes ):
229
+ return any (map (lambda x : not pd .isna (p [x ]), indexes ))
230
+
239
231
self .data_frame = data_frame
240
232
self .f = f
241
233
self .field_indexes = field_indexes
242
234
self .first_field_maybe_null = null_columns .iloc [field_indexes [0 ] - 1 ]
235
+ self ._any_not_nan = _any_not_nan
243
236
244
237
#
245
238
# prepare chunks
@@ -266,7 +259,7 @@ def serialize(self, chunk_idx: int = None):
266
259
# When the first field is null (None/NaN), we'll have
267
260
# a spurious leading comma which needs to be removed.
268
261
lp = (re .sub ('^(( |[^ ])* ),([a-zA-Z0-9])(.*)' , '\\ 1\\ 3\\ 4' , self .f (p ))
269
- for p in filter (lambda x : _any_not_nan (x , self .field_indexes ), _itertuples (chunk )))
262
+ for p in filter (lambda x : self . _any_not_nan (x , self .field_indexes ), _itertuples (chunk )))
270
263
return list (lp )
271
264
else :
272
265
return list (map (self .f , _itertuples (chunk )))
0 commit comments