Skip to content

Commit 2081fc9

Browse files
committed
Fixed time zone handling
1 parent d1adcce commit 2081fc9

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

influxdb/misc.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"""
33
Miscellaneous
44
"""
5-
from time import mktime
65

76
from .client import InfluxDBClient
87

@@ -14,6 +13,9 @@ class DataFrameClient(InfluxDBClient):
1413
The client reads and writes from pandas DataFrames.
1514
"""
1615

16+
import pandas as pd
17+
EPOCH = pd.Timestamp('1970-01-01 00:00:00.000+00:00')
18+
1719
def write_points(self, data, *args, **kwargs):
1820
"""
1921
write_points()
@@ -84,8 +86,13 @@ def _convert_dataframe_to_json(self, dataframe, name):
8486
raise TypeError('Must be DataFrame with DatetimeIndex or \
8587
PeriodIndex.')
8688
dataframe.index = dataframe.index.to_datetime()
87-
dataframe['time'] = [mktime(dt.timetuple()) for dt in dataframe.index]
89+
if dataframe.index.tzinfo == None:
90+
dataframe.index = dataframe.index.tz_localize('UTC')
91+
dataframe['time'] = [self._datetime_to_epoch(dt) for dt in dataframe.index]
8892
data = {'name': name,
8993
'columns': [str(column) for column in dataframe.columns],
9094
'points': list([list(x) for x in dataframe.values])}
9195
return data
96+
97+
def _datetime_to_epoch(self, datetime):
98+
return (datetime - DataFrameClient.EPOCH).total_seconds()

tests/influxdb/misc_test.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
import json
77
import requests_mock
88
from nose.tools import raises
9-
from datetime import datetime, timedelta
10-
import time
9+
from datetime import timedelta
1110
from tests import skipIfPYpy, using_pypy
1211

1312
if not using_pypy:
@@ -22,17 +21,16 @@
2221
class TestDataFrameClient(unittest.TestCase):
2322

2423
def test_write_points_from_dataframe(self):
25-
now = datetime(2014, 11, 15, 15, 42, 44, 543)
24+
now = pd.Timestamp('1970-01-01 00:00+00:00')
2625
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]],
2726
index=[now, now + timedelta(hours=1)],
2827
columns=["column_one", "column_two",
2928
"column_three"])
3029
points = [
3130
{
3231
"points": [
33-
["1", 1, 1.0, time.mktime(now.timetuple())],
34-
["2", 2, 2.0, time.mktime((now + timedelta(hours=1))
35-
.timetuple())]
32+
["1", 1, 1.0, 0],
33+
["2", 2, 2.0, 3600]
3634
],
3735
"name": "foo",
3836
"columns": ["column_one", "column_two", "column_three", "time"]
@@ -49,16 +47,15 @@ def test_write_points_from_dataframe(self):
4947
self.assertListEqual(json.loads(m.last_request.body), points)
5048

5149
def test_write_points_from_dataframe_with_numeric_column_names(self):
52-
now = datetime(2014, 11, 15, 15, 42, 44, 543)
50+
now = pd.Timestamp('1970-01-01 00:00+00:00')
5351
# df with numeric column names
5452
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]],
5553
index=[now, now + timedelta(hours=1)])
5654
points = [
5755
{
5856
"points": [
59-
["1", 1, 1.0, time.mktime(now.timetuple())],
60-
["2", 2, 2.0, time.mktime((now + timedelta(hours=1))
61-
.timetuple())]
57+
["1", 1, 1.0, 0],
58+
["2", 2, 2.0, 3600]
6259
],
6360
"name": "foo",
6461
"columns": ['0', '1', '2', "time"]
@@ -75,18 +72,16 @@ def test_write_points_from_dataframe_with_numeric_column_names(self):
7572
self.assertListEqual(json.loads(m.last_request.body), points)
7673

7774
def test_write_points_from_dataframe_with_period_index(self):
78-
now = datetime(2014, 11, 16)
7975
dataframe = pd.DataFrame(data=[["1", 1, 1.0], ["2", 2, 2.0]],
80-
index=[pd.Period('2014-11-16'),
81-
pd.Period('2014-11-17')],
76+
index=[pd.Period('1970-01-01'),
77+
pd.Period('1970-01-02')],
8278
columns=["column_one", "column_two",
8379
"column_three"])
8480
points = [
8581
{
8682
"points": [
87-
["1", 1, 1.0, time.mktime(now.timetuple())],
88-
["2", 2, 2.0, time.mktime((now + timedelta(hours=24))
89-
.timetuple())]
83+
["1", 1, 1.0, 0],
84+
["2", 2, 2.0, 86400]
9085
],
9186
"name": "foo",
9287
"columns": ["column_one", "column_two", "column_three", "time"]
@@ -117,7 +112,7 @@ def test_write_points_from_dataframe_fails_without_time_index(self):
117112

118113
@raises(TypeError)
119114
def test_write_points_from_dataframe_fails_with_series(self):
120-
now = datetime(2014, 11, 16)
115+
now = pd.Timestamp('1970-01-01 00:00+00:00')
121116
dataframe = pd.Series(data=[1.0, 2.0],
122117
index=[now, now + timedelta(hours=1)])
123118

0 commit comments

Comments
 (0)