Skip to content

Commit 1c5fed3

Browse files
author
Grégory Starck
committed
Slightly better dataframe_client.
1 parent fb273cd commit 1c5fed3

File tree

3 files changed

+47
-28
lines changed

3 files changed

+47
-28
lines changed

influxdb/_dataframe_client.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77

88
from .client import InfluxDBClient
99

10-
try:
11-
import pandas as pd
12-
except ImportError:
13-
pd = None
10+
import pandas as pd
1411

1512

1613
class DataFrameClient(InfluxDBClient):
@@ -20,14 +17,7 @@ class DataFrameClient(InfluxDBClient):
2017
The client reads and writes from pandas DataFrames.
2118
"""
2219

23-
def __init__(self, *args, **kwargs):
24-
super(DataFrameClient, self).__init__(*args, **kwargs)
25-
if not pd:
26-
raise ImportError(
27-
'DataFrameClient requires Pandas'
28-
)
29-
30-
self.EPOCH = pd.Timestamp('1970-01-01 00:00:00.000+00:00')
20+
EPOCH = pd.Timestamp('1970-01-01 00:00:00.000+00:00')
3121

3222
def write_points(self, data, *args, **kwargs):
3323
"""
@@ -56,13 +46,15 @@ def write_points(self, data, *args, **kwargs):
5646
name=key,
5747
dataframe=data_frame.ix[start_index:end_index].copy(),
5848
time_precision=time_precision)]
59-
InfluxDBClient.write_points(self, data, *args, **kwargs)
49+
super(DataFrameClient, self).write_points(data,
50+
*args, **kwargs)
6051
return True
6152
else:
6253
data = [self._convert_dataframe_to_json(
6354
name=key, dataframe=dataframe, time_precision=time_precision)
6455
for key, dataframe in data.items()]
65-
return InfluxDBClient.write_points(self, data, *args, **kwargs)
56+
return super(DataFrameClient, self).write_points(data,
57+
*args, **kwargs)
6658

6759
def write_points_with_precision(self, data, time_precision='s'):
6860
"""
@@ -86,14 +78,11 @@ def query(self, query, time_precision='s', chunked=False):
8678
retrieved in chunks, False otherwise.
8779
8880
"""
89-
result = super(DataFrameClient, self).query(
90-
query=query,
91-
time_precision=time_precision,
92-
chunked=chunked)
93-
if len(result['results'][0]) > 0:
94-
return self._to_dataframe(result['results'][0], time_precision)
81+
results = super(DataFrameClient, self).query(query, database=database)
82+
if len(results) > 0:
83+
return self._to_dataframe(results, time_precision)
9584
else:
96-
return result
85+
return results
9786

9887
def _to_dataframe(self, json_result, time_precision):
9988
dataframe = pd.DataFrame(data=json_result['points'],

influxdb/dataframe_client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
DataFrame client for InfluxDB
4+
"""
5+
6+
__all__ = ['DataFrameClient']
7+
8+
try:
9+
import pandas
10+
del pandas
11+
except ImportError as err:
12+
from .client import InfluxDBClient
13+
14+
class DataFrameClient(InfluxDBClient):
15+
def __init__(self, *a, **kw):
16+
raise ImportError("DataFrameClient requires Pandas "
17+
"which couldn't be imported: %s" % err)
18+
else:
19+
from ._dataframe_client import DataFrameClient

tests/influxdb/dataframe_client_test.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,24 @@ def test_query_with_empty_result(self):
211211
assert result == []
212212

213213
def test_list_series(self):
214-
response = [
215-
{
216-
'columns': ['time', 'name'],
217-
'name': 'list_series_result',
218-
'points': [[0, 'seriesA'], [0, 'seriesB']]
219-
}
220-
]
214+
response = {
215+
'results': [
216+
{
217+
'series': [{
218+
'columns': ['id'],
219+
'name': 'seriesA',
220+
'values': [[0]],
221+
}]
222+
},
223+
{
224+
'series': [{
225+
'columns': ['id'],
226+
'name': 'seriesB',
227+
'values': [[1]],
228+
}]
229+
},
230+
]
231+
}
221232
with _mocked_session('get', 200, response):
222233
cli = DataFrameClient('host', 8086, 'username', 'password', 'db')
223234
series_list = cli.get_list_series()

0 commit comments

Comments
 (0)