Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Bugfix dropna in DataFrameClient. Add unit test. #778

Merged
merged 1 commit into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion influxdb/_dataframe_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ def query(self,
def _to_dataframe(self, rs, dropna=True):
result = defaultdict(list)
if isinstance(rs, list):
return map(self._to_dataframe, rs)
return map(self._to_dataframe, rs,
[dropna for _ in range(len(rs))])

for key, data in rs.items():
name, tags = key
Expand Down
92 changes: 92 additions & 0 deletions influxdb/tests/dataframe_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,98 @@ def test_multiquery_into_dataframe(self):
for k in e:
assert_frame_equal(e[k], r[k])

def test_multiquery_into_dataframe_dropna(self):
"""Test multiquery into df for TestDataFrameClient object."""
data = {
"results": [
{
"series": [
{
"name": "cpu_load_short",
"columns": ["time", "value", "value2", "value3"],
"values": [
["2015-01-29T21:55:43.702900257Z",
0.55, 0.254, numpy.NaN],
["2015-01-29T21:55:43.702900257Z",
23422, 122878, numpy.NaN],
["2015-06-11T20:46:02Z",
0.64, 0.5434, numpy.NaN]
]
}
]
}, {
"series": [
{
"name": "cpu_load_short",
"columns": ["time", "count"],
"values": [
["1970-01-01T00:00:00Z", 3]
]
}
]
}
]
}

pd1 = pd.DataFrame(
[[0.55, 0.254, numpy.NaN],
[23422.0, 122878, numpy.NaN],
[0.64, 0.5434, numpy.NaN]],
columns=['value', 'value2', 'value3'],
index=pd.to_datetime([
"2015-01-29 21:55:43.702900257+0000",
"2015-01-29 21:55:43.702900257+0000",
"2015-06-11 20:46:02+0000"]))

if pd1.index.tzinfo is None:
pd1.index = pd1.index.tz_localize('UTC')

pd1_dropna = pd.DataFrame(
[[0.55, 0.254], [23422.0, 122878], [0.64, 0.5434]],
columns=['value', 'value2'],
index=pd.to_datetime([
"2015-01-29 21:55:43.702900257+0000",
"2015-01-29 21:55:43.702900257+0000",
"2015-06-11 20:46:02+0000"]))

if pd1_dropna.index.tzinfo is None:
pd1_dropna.index = pd1_dropna.index.tz_localize('UTC')

pd2 = pd.DataFrame(
[[3]], columns=['count'],
index=pd.to_datetime(["1970-01-01 00:00:00+00:00"]))

if pd2.index.tzinfo is None:
pd2.index = pd2.index.tz_localize('UTC')

expected_dropna_true = [
{'cpu_load_short': pd1_dropna},
{'cpu_load_short': pd2}]
expected_dropna_false = [
{'cpu_load_short': pd1},
{'cpu_load_short': pd2}]

cli = DataFrameClient('host', 8086, 'username', 'password', 'db')
iql = "SELECT value FROM cpu_load_short WHERE region=$region;" \
"SELECT count(value) FROM cpu_load_short WHERE region=$region"
bind_params = {'region': 'us-west'}

for dropna in [True, False]:
with _mocked_session(cli, 'GET', 200, data):
result = cli.query(iql, bind_params=bind_params, dropna=dropna)
expected = \
expected_dropna_true if dropna else expected_dropna_false
for r, e in zip(result, expected):
for k in e:
assert_frame_equal(e[k], r[k])

# test default value (dropna = True)
with _mocked_session(cli, 'GET', 200, data):
result = cli.query(iql, bind_params=bind_params)
for r, e in zip(result, expected_dropna_true):
for k in e:
assert_frame_equal(e[k], r[k])

def test_query_with_empty_result(self):
"""Test query with empty results in TestDataFrameClient object."""
cli = DataFrameClient('host', 8086, 'username', 'password', 'db')
Expand Down