13
13
import numpy as np
14
14
15
15
from .client import InfluxDBClient
16
- from .line_protocol import _escape_tag
16
+ from .line_protocol import _escape_tag , _to_nanos
17
17
18
18
19
19
def _pandas_time_unit (time_precision ):
@@ -267,19 +267,19 @@ def _convert_dataframe_to_json(dataframe,
267
267
268
268
precision_factor = {
269
269
"n" : 1 ,
270
- "u" : 1e3 ,
271
- "ms" : 1e6 ,
272
- "s" : 1e9 ,
273
- "m" : 1e9 * 60 ,
274
- "h" : 1e9 * 3600 ,
270
+ "u" : 10 ** 3 ,
271
+ "ms" : 10 ** 6 ,
272
+ "s" : 10 ** 9 ,
273
+ "m" : 10 ** 9 * 60 ,
274
+ "h" : 10 ** 9 * 3600 ,
275
275
}.get (time_precision , 1 )
276
276
277
277
if not tag_columns :
278
278
points = [
279
279
{'measurement' : measurement ,
280
280
'fields' :
281
281
rec .replace ([np .inf , - np .inf ], np .nan ).dropna ().to_dict (),
282
- 'time' : np .int64 (ts .value / precision_factor ) }
282
+ 'time' : np .int64 (ts .value ) // precision_factor }
283
283
for ts , (_ , rec ) in zip (
284
284
dataframe .index ,
285
285
dataframe [field_columns ].iterrows ()
@@ -293,7 +293,7 @@ def _convert_dataframe_to_json(dataframe,
293
293
'tags' : dict (list (tag .items ()) + list (tags .items ())),
294
294
'fields' :
295
295
rec .replace ([np .inf , - np .inf ], np .nan ).dropna ().to_dict (),
296
- 'time' : np .int64 (ts .value / precision_factor ) }
296
+ 'time' : np .int64 (ts .value ) // precision_factor }
297
297
for ts , tag , (_ , rec ) in zip (
298
298
dataframe .index ,
299
299
dataframe [tag_columns ].to_dict ('record' ),
@@ -354,20 +354,20 @@ def _convert_dataframe_to_lines(self,
354
354
355
355
precision_factor = {
356
356
"n" : 1 ,
357
- "u" : 1e3 ,
358
- "ms" : 1e6 ,
359
- "s" : 1e9 ,
360
- "m" : 1e9 * 60 ,
361
- "h" : 1e9 * 3600 ,
357
+ "u" : 10 ** 3 ,
358
+ "ms" : 10 ** 6 ,
359
+ "s" : 10 ** 9 ,
360
+ "m" : 10 ** 9 * 60 ,
361
+ "h" : 10 ** 9 * 3600 ,
362
362
}.get (time_precision , 1 )
363
363
364
364
# Make array of timestamp ints
365
365
if isinstance (dataframe .index , pd .PeriodIndex ):
366
- time = ((dataframe .index .to_timestamp ().values .astype (np .int64 ) /
367
- precision_factor ).astype (np . int64 ). astype ( str ))
366
+ time = ((dataframe .index .to_timestamp ().values .astype (np .int64 ) //
367
+ precision_factor ).astype (str ))
368
368
else :
369
- time = ((pd .to_datetime (dataframe .index ).values .astype (np .int64 ) /
370
- precision_factor ).astype (np . int64 ). astype ( str ))
369
+ time = ((pd .to_datetime (dataframe .index ).values .astype (np .int64 ) //
370
+ precision_factor ).astype (str ))
371
371
372
372
# If tag columns exist, make an array of formatted tag keys and values
373
373
if tag_columns :
@@ -473,16 +473,13 @@ def _stringify_dataframe(dframe, numeric_precision, datatype='field'):
473
473
return dframe
474
474
475
475
def _datetime_to_epoch (self , datetime , time_precision = 's' ):
476
- seconds = (datetime - self .EPOCH ).total_seconds ()
477
- if time_precision == 'h' :
478
- return seconds / 3600
479
- elif time_precision == 'm' :
480
- return seconds / 60
481
- elif time_precision == 's' :
482
- return seconds
483
- elif time_precision == 'ms' :
484
- return seconds * 1e3
485
- elif time_precision == 'u' :
486
- return seconds * 1e6
487
- elif time_precision == 'n' :
488
- return seconds * 1e9
476
+ nanos = _to_nanos (datetime )
477
+ precision_factor = {
478
+ "n" : 1 ,
479
+ "u" : 10 ** 3 ,
480
+ "ms" : 10 ** 6 ,
481
+ "s" : 10 ** 9 ,
482
+ "m" : 10 ** 9 * 60 ,
483
+ "h" : 10 ** 9 * 3600 ,
484
+ }.get (time_precision , 1 )
485
+ return nanos // precision_factor
0 commit comments