Skip to content

Commit c35e49e

Browse files
authored
Bugfix dropna in DataFrameClient. Add unit test. (influxdata#778)
1 parent 72c372f commit c35e49e

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

influxdb/_dataframe_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ def query(self,
203203
def _to_dataframe(self, rs, dropna=True):
204204
result = defaultdict(list)
205205
if isinstance(rs, list):
206-
return map(self._to_dataframe, rs)
206+
return map(self._to_dataframe, rs,
207+
[dropna for _ in range(len(rs))])
207208

208209
for key, data in rs.items():
209210
name, tags = key

influxdb/tests/dataframe_client_test.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,98 @@ def test_multiquery_into_dataframe(self):
968968
for k in e:
969969
assert_frame_equal(e[k], r[k])
970970

971+
def test_multiquery_into_dataframe_dropna(self):
972+
"""Test multiquery into df for TestDataFrameClient object."""
973+
data = {
974+
"results": [
975+
{
976+
"series": [
977+
{
978+
"name": "cpu_load_short",
979+
"columns": ["time", "value", "value2", "value3"],
980+
"values": [
981+
["2015-01-29T21:55:43.702900257Z",
982+
0.55, 0.254, numpy.NaN],
983+
["2015-01-29T21:55:43.702900257Z",
984+
23422, 122878, numpy.NaN],
985+
["2015-06-11T20:46:02Z",
986+
0.64, 0.5434, numpy.NaN]
987+
]
988+
}
989+
]
990+
}, {
991+
"series": [
992+
{
993+
"name": "cpu_load_short",
994+
"columns": ["time", "count"],
995+
"values": [
996+
["1970-01-01T00:00:00Z", 3]
997+
]
998+
}
999+
]
1000+
}
1001+
]
1002+
}
1003+
1004+
pd1 = pd.DataFrame(
1005+
[[0.55, 0.254, numpy.NaN],
1006+
[23422.0, 122878, numpy.NaN],
1007+
[0.64, 0.5434, numpy.NaN]],
1008+
columns=['value', 'value2', 'value3'],
1009+
index=pd.to_datetime([
1010+
"2015-01-29 21:55:43.702900257+0000",
1011+
"2015-01-29 21:55:43.702900257+0000",
1012+
"2015-06-11 20:46:02+0000"]))
1013+
1014+
if pd1.index.tzinfo is None:
1015+
pd1.index = pd1.index.tz_localize('UTC')
1016+
1017+
pd1_dropna = pd.DataFrame(
1018+
[[0.55, 0.254], [23422.0, 122878], [0.64, 0.5434]],
1019+
columns=['value', 'value2'],
1020+
index=pd.to_datetime([
1021+
"2015-01-29 21:55:43.702900257+0000",
1022+
"2015-01-29 21:55:43.702900257+0000",
1023+
"2015-06-11 20:46:02+0000"]))
1024+
1025+
if pd1_dropna.index.tzinfo is None:
1026+
pd1_dropna.index = pd1_dropna.index.tz_localize('UTC')
1027+
1028+
pd2 = pd.DataFrame(
1029+
[[3]], columns=['count'],
1030+
index=pd.to_datetime(["1970-01-01 00:00:00+00:00"]))
1031+
1032+
if pd2.index.tzinfo is None:
1033+
pd2.index = pd2.index.tz_localize('UTC')
1034+
1035+
expected_dropna_true = [
1036+
{'cpu_load_short': pd1_dropna},
1037+
{'cpu_load_short': pd2}]
1038+
expected_dropna_false = [
1039+
{'cpu_load_short': pd1},
1040+
{'cpu_load_short': pd2}]
1041+
1042+
cli = DataFrameClient('host', 8086, 'username', 'password', 'db')
1043+
iql = "SELECT value FROM cpu_load_short WHERE region=$region;" \
1044+
"SELECT count(value) FROM cpu_load_short WHERE region=$region"
1045+
bind_params = {'region': 'us-west'}
1046+
1047+
for dropna in [True, False]:
1048+
with _mocked_session(cli, 'GET', 200, data):
1049+
result = cli.query(iql, bind_params=bind_params, dropna=dropna)
1050+
expected = \
1051+
expected_dropna_true if dropna else expected_dropna_false
1052+
for r, e in zip(result, expected):
1053+
for k in e:
1054+
assert_frame_equal(e[k], r[k])
1055+
1056+
# test default value (dropna = True)
1057+
with _mocked_session(cli, 'GET', 200, data):
1058+
result = cli.query(iql, bind_params=bind_params)
1059+
for r, e in zip(result, expected_dropna_true):
1060+
for k in e:
1061+
assert_frame_equal(e[k], r[k])
1062+
9711063
def test_query_with_empty_result(self):
9721064
"""Test query with empty results in TestDataFrameClient object."""
9731065
cli = DataFrameClient('host', 8086, 'username', 'password', 'db')

0 commit comments

Comments
 (0)