Skip to content

Commit bd10016

Browse files
committed
[Fix](influxdata#649) changes to support ns timepoints
extensive but not fully tested, unittests updates to follow also fixes ( influxdata#527,influxdata#489, influxdata#346, influxdata#344, influxdata#340)
1 parent caa9e7d commit bd10016

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

influxdb/_dataframe_client.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def _convert_dataframe_to_json(dataframe,
261261
{'measurement': measurement,
262262
'tags': dict(list(tag.items()) + list(tags.items())),
263263
'fields': rec,
264-
'time': np.int64(ts.value / precision_factor)}
264+
'time': np.int64(ts.value // precision_factor)}
265265
for ts, tag, rec in zip(dataframe.index,
266266
dataframe[tag_columns].to_dict('record'),
267267
dataframe[field_columns].to_dict('record'))
@@ -329,10 +329,10 @@ def _convert_dataframe_to_lines(self,
329329

330330
# Make array of timestamp ints
331331
if isinstance(dataframe.index, pd.PeriodIndex):
332-
time = ((dataframe.index.to_timestamp().values.astype(np.int64) /
332+
time = ((dataframe.index.to_timestamp().values.astype(np.int64) //
333333
precision_factor).astype(np.int64).astype(str))
334334
else:
335-
time = ((pd.to_datetime(dataframe.index).values.astype(np.int64) /
335+
time = ((pd.to_datetime(dataframe.index).values.astype(np.int64) //
336336
precision_factor).astype(np.int64).astype(str))
337337

338338
# If tag columns exist, make an array of formatted tag keys and values
@@ -439,16 +439,16 @@ def _stringify_dataframe(dframe, numeric_precision, datatype='field'):
439439
return dframe
440440

441441
def _datetime_to_epoch(self, datetime, time_precision='s'):
442-
seconds = (datetime - self.EPOCH).total_seconds()
442+
nanoseconds = (datetime - self.EPOCH).value
443443
if time_precision == 'h':
444-
return seconds / 3600
444+
return np.int64(nanoseconds // 1e9 // 3600)
445445
elif time_precision == 'm':
446-
return seconds / 60
446+
return np.int64(nanoseconds // 1e9 // 60)
447447
elif time_precision == 's':
448-
return seconds
448+
return np.int64(nanoseconds // 1e9)
449449
elif time_precision == 'ms':
450-
return seconds * 1e3
450+
return np.int64(nanoseconds // 1e6)
451451
elif time_precision == 'u':
452-
return seconds * 1e6
452+
return np.int64(nanoseconds // 1e3)
453453
elif time_precision == 'n':
454-
return seconds * 1e9
454+
return nanoseconds

influxdb/line_protocol.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,39 @@
1313
from dateutil.parser import parse
1414
from six import iteritems, binary_type, text_type, integer_types, PY2
1515

16-
EPOCH = UTC.localize(datetime.utcfromtimestamp(0))
16+
import pandas as pd # Provide for ns timestamps
17+
18+
EPOCH = pd.Timestamp(0, tz='UTC')
1719

1820

1921
def _convert_timestamp(timestamp, precision=None):
2022
if isinstance(timestamp, Integral):
2123
return timestamp # assume precision is correct if timestamp is int
2224

2325
if isinstance(_get_unicode(timestamp), text_type):
24-
timestamp = parse(timestamp)
26+
timestamp = pd.Timestamp(timestamp)
2527

2628
if isinstance(timestamp, datetime):
2729
if not timestamp.tzinfo:
28-
timestamp = UTC.localize(timestamp)
30+
timestamp = pd.Timestamp(timestamp, tz='UTC')
2931

30-
ns = (timestamp - EPOCH).total_seconds() * 1e9
32+
if isinstance(timestamp, pd._libs.tslib.Timestamp):
33+
if not timestamp.tzinfo:
34+
timestamp = pd.Timestamp(timestamp, tz = 'UTC')
35+
36+
ns = (timestamp - EPOCH).value
3137
if precision is None or precision == 'n':
3238
return ns
3339
elif precision == 'u':
34-
return ns / 1e3
40+
return ns // 1e3
3541
elif precision == 'ms':
36-
return ns / 1e6
42+
return ns // 1e6
3743
elif precision == 's':
38-
return ns / 1e9
44+
return ns // 1e9
3945
elif precision == 'm':
40-
return ns / 1e9 / 60
46+
return ns // 1e9 // 60
4147
elif precision == 'h':
42-
return ns / 1e9 / 3600
48+
return ns // 1e9 // 3600
4349

4450
raise ValueError(timestamp)
4551

0 commit comments

Comments
 (0)