@@ -132,7 +132,7 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
132
132
keys = []
133
133
# tags holds a list of tag f-string segments ordered alphabetically by tag key.
134
134
tags = []
135
- # fields holds a list of field f-string segments ordered alphebetically by field key
135
+ # fields holds a list of field f-string segments ordered alphabetically by field key
136
136
fields = []
137
137
# field_indexes holds the index into each row of all the fields.
138
138
field_indexes = []
@@ -160,6 +160,11 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
160
160
# null_columns has a bool value for each column holding
161
161
# whether that column contains any null (NaN or None) values.
162
162
null_columns = data_frame .isnull ().any ()
163
+
164
+ # inf_columns has a bool value for each column holding
165
+ # whether that column contains any Inf values.
166
+ inf_columns = data_frame .isin ([np .inf , - np .inf ]).any ()
167
+
163
168
timestamp_index = 0
164
169
165
170
# Iterate through the columns building up the expression for each column.
@@ -175,9 +180,10 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
175
180
176
181
if key in data_frame_tag_columns :
177
182
# This column is a tag column.
178
- if null_columns .iloc [index ]:
183
+ if null_columns .iloc [index ] or inf_columns . iloc [ index ] :
179
184
key_value = f"""{{
180
- '' if { val_format } == '' or pd.isna({ val_format } ) else
185
+ '' if { val_format } == '' or pd.isna({ val_format } ) or
186
+ ({ inf_columns .iloc [index ]} and np.isinf({ val_format } )) else
181
187
f',{ key_format } ={{str({ val_format } ).translate(_ESCAPE_STRING)}}'
182
188
}}"""
183
189
else :
@@ -199,16 +205,17 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
199
205
if (issubclass (value .type , np .integer ) or issubclass (value .type , np .floating ) or
200
206
issubclass (value .type , np .bool_ )):
201
207
suffix = 'i' if issubclass (value .type , np .integer ) else ''
202
- if null_columns .iloc [index ]:
208
+ if null_columns .iloc [index ] or inf_columns . iloc [ index ] :
203
209
field_value = (
204
- f"""{{"" if pd.isna({ val_format } ) else f"{ sep } { key_format } ={{{ val_format } }}{ suffix } "}}"""
210
+ f"""{{"" if pd.isna({ val_format } ) or ({ inf_columns .iloc [index ]} and np.isinf({ val_format } )) else
211
+ f"{ sep } { key_format } ={{{ val_format } }}{ suffix } "}}"""
205
212
)
206
213
else :
207
214
field_value = f'{ sep } { key_format } ={{{ val_format } }}{ suffix } '
208
215
else :
209
- if null_columns .iloc [index ]:
216
+ if null_columns .iloc [index ] or inf_columns . iloc [ index ] :
210
217
field_value = f"""{{
211
- '' if pd.isna({ val_format } ) else
218
+ '' if pd.isna({ val_format } ) or ( { inf_columns . iloc [ index ] } and np.isinf( { val_format } )) else
212
219
f'{ sep } { key_format } ="{{str({ val_format } ).translate(_ESCAPE_STRING)}}"'
213
220
}}"""
214
221
else :
@@ -234,11 +241,12 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
234
241
'_ESCAPE_STRING' : _ESCAPE_STRING ,
235
242
'keys' : keys ,
236
243
'pd' : pd ,
244
+ 'np' : np ,
237
245
})
238
246
239
247
for k , v in dict (data_frame .dtypes ).items ():
240
248
if k in data_frame_tag_columns :
241
- data_frame [k ]. replace ( '' , np .nan , inplace = True )
249
+ data_frame [k ] = data_frame [ k ]. apply ( lambda x : np .nan if x == '' else x )
242
250
243
251
self .data_frame = data_frame
244
252
self .f = f
0 commit comments