Skip to content

Commit

Permalink
feat: it there is no results than return empty DataFrame (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
bednar committed Nov 13, 2019
1 parent 8dfc831 commit 35ced8e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion influxdb_client/client/query_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import csv
from typing import List, Generator, Any

from pandas import DataFrame

from influxdb_client import Dialect
from influxdb_client import Query, QueryService
from influxdb_client.client.flux_csv_parser import FluxCsvParser, FluxSerializationMode
Expand Down Expand Up @@ -112,7 +114,9 @@ def query_data_frame(self, query: str, org=None, data_frame_index: List[str] = N
data_frame_index=data_frame_index)
_dataFrames = list(_parser.generator())

if len(_dataFrames) == 1:
if len(_dataFrames) == 0:
return DataFrame(columns=[], index=None)
elif len(_dataFrames) == 1:
return _dataFrames[0]
else:
return _dataFrames
Expand Down
18 changes: 18 additions & 0 deletions tests/test_QueryApiDataFrame.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ def test_more_table(self):
self.assertListEqual([0, 1, 2, 3, 4], list(_dataFrames[2].index))
self.assertEqual(5, len(_dataFrames[2]))

def test_empty_data_set(self):
query_response = '\n'

httpretty.register_uri(httpretty.POST, uri="http://localhost/api/v2/query", status=200, body=query_response)

self.client = InfluxDBClient("http://localhost", "my-token", org="my-org", enable_gzip=False)

_dataFrame = self.client.query_api().query_data_frame(
'from(bucket: "my-bucket") '
'|> range(start: -5s, stop: now()) '
'|> filter(fn: (r) => r._measurement == "mem") '
'|> filter(fn: (r) => r._field == "not_exit")',
"my-org")

self.assertEqual(DataFrame, type(_dataFrame))
self.assertListEqual([], list(_dataFrame.columns))
self.assertListEqual([], list(_dataFrame.index))

def test_more_table_custom_index(self):
query_response = \
'#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,long,string,string,string\n' \
Expand Down

0 comments on commit 35ced8e

Please sign in to comment.