@@ -15,18 +15,17 @@ class DataFrameClient(InfluxDBClient):
15
15
The client reads and writes from pandas DataFrames.
16
16
"""
17
17
18
- def __init__ (self , * args , ** kwargs ):
18
+ def __init__ (self , ignore_nan = True , * args , ** kwargs ):
19
19
super (DataFrameClient , self ).__init__ (* args , ** kwargs )
20
+
20
21
try :
21
22
global pd
22
23
import pandas as pd
23
24
except ImportError as ex :
24
- raise ImportError (
25
- 'DataFrameClient requires Pandas, "{ex}" problem importing'
26
- .format (ex = str (ex ))
27
- )
28
-
25
+ raise ImportError ('DataFrameClient requires Pandas, '
26
+ '"{ex}" problem importing' .format (ex = str (ex )))
29
27
self .EPOCH = pd .Timestamp ('1970-01-01 00:00:00.000+00:00' )
28
+ self .ignore_nan = ignore_nan
30
29
31
30
def write_points (self , data , * args , ** kwargs ):
32
31
"""
@@ -135,9 +134,24 @@ def _convert_dataframe_to_json(self, dataframe, name, time_precision='s'):
135
134
for dt in dataframe .index ]
136
135
data = {'name' : name ,
137
136
'columns' : [str (column ) for column in dataframe .columns ],
138
- 'points' : list ([ list (x ) for x in dataframe .values ]) }
137
+ 'points' : [ self . _convert_array (x ) for x in dataframe .values ]}
139
138
return data
140
139
140
+ def _convert_array (self , array ):
141
+ try :
142
+ global np
143
+ import numpy as np
144
+ except ImportError as ex :
145
+ raise ImportError ('DataFrameClient requires Numpy, '
146
+ '"{ex}" problem importing' .format (ex = str (ex )))
147
+ if self .ignore_nan :
148
+ number_types = (int , float , np .number )
149
+ condition = (all (isinstance (el , number_types ) for el in array ) and
150
+ np .isnan (array ))
151
+ return list (np .where (condition , None , array ))
152
+ else :
153
+ return list (array )
154
+
141
155
def _datetime_to_epoch (self , datetime , time_precision = 's' ):
142
156
seconds = (datetime - self .EPOCH ).total_seconds ()
143
157
if time_precision == 's' :
0 commit comments