Skip to content

Commit 100e5c3

Browse files
authored
precision_factor should be an int, not a float
might fix influxdata#822
1 parent 95e0efb commit 100e5c3

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

influxdb/_dataframe_client.py

+27-30
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import numpy as np
1414

1515
from .client import InfluxDBClient
16-
from .line_protocol import _escape_tag
16+
from .line_protocol import _escape_tag, _to_nanos
1717

1818

1919
def _pandas_time_unit(time_precision):
@@ -267,19 +267,19 @@ def _convert_dataframe_to_json(dataframe,
267267

268268
precision_factor = {
269269
"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,
275275
}.get(time_precision, 1)
276276

277277
if not tag_columns:
278278
points = [
279279
{'measurement': measurement,
280280
'fields':
281281
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}
283283
for ts, (_, rec) in zip(
284284
dataframe.index,
285285
dataframe[field_columns].iterrows()
@@ -293,7 +293,7 @@ def _convert_dataframe_to_json(dataframe,
293293
'tags': dict(list(tag.items()) + list(tags.items())),
294294
'fields':
295295
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}
297297
for ts, tag, (_, rec) in zip(
298298
dataframe.index,
299299
dataframe[tag_columns].to_dict('record'),
@@ -354,20 +354,20 @@ def _convert_dataframe_to_lines(self,
354354

355355
precision_factor = {
356356
"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,
362362
}.get(time_precision, 1)
363363

364364
# Make array of timestamp ints
365365
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))
368368
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))
371371

372372
# If tag columns exist, make an array of formatted tag keys and values
373373
if tag_columns:
@@ -473,16 +473,13 @@ def _stringify_dataframe(dframe, numeric_precision, datatype='field'):
473473
return dframe
474474

475475
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

Comments
 (0)