Skip to content

Commit 3745d57

Browse files
committed
Merge pull request influxdata#123 from timtroendle/feature-multiple-time-series-to-dataframe
Feature multiple time series to dataframe (Thanks @timtroendle!)
2 parents 6ec779d + d7013fe commit 3745d57

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

influxdb/dataframe_client.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ def write_points_with_precision(self, data, time_precision='s'):
7777

7878
def query(self, query, time_precision='s', chunked=False):
7979
"""
80-
Quering data into a DataFrame.
80+
Quering data into DataFrames.
81+
82+
Returns a DataFrame for a single time series and a map for multiple
83+
time series with the time series as value and its name as key.
8184
8285
:param time_precision: [Optional, default 's'] Either 's', 'm', 'ms'
8386
or 'u'.
@@ -88,10 +91,14 @@ def query(self, query, time_precision='s', chunked=False):
8891
result = InfluxDBClient.query(self, query=query,
8992
time_precision=time_precision,
9093
chunked=chunked)
91-
if len(result) > 0:
94+
if len(result) == 0:
95+
return result
96+
elif len(result) == 1:
9297
return self._to_dataframe(result[0], time_precision)
9398
else:
94-
return result
99+
return {time_series['name']: self._to_dataframe(time_series,
100+
time_precision)
101+
for time_series in result}
95102

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

tests/influxdb/dataframe_client_test.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,46 @@ def test_query_into_dataframe(self):
203203
result = cli.query('select column_one from foo;')
204204
assert_frame_equal(dataframe, result)
205205

206+
def test_query_multiple_time_series(self):
207+
data = [
208+
{
209+
"name": "series1",
210+
"columns": ["time", "mean", "min", "max", "stddev"],
211+
"points": [[0, 323048, 323048, 323048, 0]]
212+
},
213+
{
214+
"name": "series2",
215+
"columns": ["time", "mean", "min", "max", "stddev"],
216+
"points": [[0, -2.8233, -2.8503, -2.7832, 0.0173]]
217+
},
218+
{
219+
"name": "series3",
220+
"columns": ["time", "mean", "min", "max", "stddev"],
221+
"points": [[0, -0.01220, -0.01220, -0.01220, 0]]
222+
}
223+
]
224+
dataframes = {
225+
'series1': pd.DataFrame(data=[[323048, 323048, 323048, 0]],
226+
index=pd.to_datetime([0], unit='s',
227+
utc=True),
228+
columns=['mean', 'min', 'max', 'stddev']),
229+
'series2': pd.DataFrame(data=[[-2.8233, -2.8503, -2.7832, 0.0173]],
230+
index=pd.to_datetime([0], unit='s',
231+
utc=True),
232+
columns=['mean', 'min', 'max', 'stddev']),
233+
'series3': pd.DataFrame(data=[[-0.01220, -0.01220, -0.01220, 0]],
234+
index=pd.to_datetime([0], unit='s',
235+
utc=True),
236+
columns=['mean', 'min', 'max', 'stddev'])
237+
}
238+
with _mocked_session('get', 200, data):
239+
cli = DataFrameClient('host', 8086, 'username', 'password', 'db')
240+
result = cli.query("""select mean(value), min(value), max(value),
241+
stddev(value) from series1, series2, series3""")
242+
assert dataframes.keys() == result.keys()
243+
for key in dataframes.keys():
244+
assert_frame_equal(dataframes[key], result[key])
245+
206246
def test_query_with_empty_result(self):
207247
with _mocked_session('get', 200, []):
208248
cli = DataFrameClient('host', 8086, 'username', 'password', 'db')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy