Skip to content

Commit

Permalink
feat: Prepare documentation for PandasData frame (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
bednar committed Nov 13, 2019
1 parent 3193442 commit 47be9d2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
45 changes: 42 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,50 @@ The API also support streaming ``FluxRecord`` via `query_stream <https://github.
client.__del__()
Pandas DataFrame
^^^^^^^^^^^^^^^^
----------------
.. marker-pandas-start
.. note::
Note that if a query returns more then one table than the client generates a DataFrame for each of them.
.. note:: Note that if a query returns more then one table than the client generates a DataFrame for each of them.

The ``client`` is able to retrieve data in `Pandas DataFrame <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html>`_ format thought ``query_data_frame``:

.. code-block:: python
from influxdb_client import InfluxDBClient, Point, Dialect
from influxdb_client.client.write_api import SYNCHRONOUS
client = InfluxDBClient(url="http://localhost:9999", token="my-token", org="my-org")
write_api = client.write_api(write_options=SYNCHRONOUS)
query_api = client.query_api()
"""
Prepare data
"""
_point1 = Point("my_measurement").tag("location", "Prague").field("temperature", 25.3)
_point2 = Point("my_measurement").tag("location", "New York").field("temperature", 24.3)
write_api.write(bucket="my-bucket", org="my-org", record=[_point1, _point2])
"""
Query: using Pandas DataFrame
"""
data_frame = query_api.query_data_frame('from(bucket:"my-bucket") '
'|> range(start: -10m) '
'|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value") '
'|> keep(columns: ["location", "temperature"])')
print(data_frame.to_string())
"""
Close client
"""
client.__del__()
.. code-block::
result table location temperature
0 _result 0 New York 24.3
1 _result 1 Prague 25.3
.. marker-pandas-end
Expand Down
12 changes: 12 additions & 0 deletions examples/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@
if not len(csv_line) == 0:
print(f'Temperature in {csv_line[9]} is {csv_line[6]}')

print()
print()

"""
Query: using Pandas DataFrame
"""
data_frame = query_api.query_data_frame('from(bucket:"my-bucket") '
'|> range(start: -10m) '
'|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value") '
'|> keep(columns: ["location", "temperature"])')
print(data_frame.to_string())

"""
Close client
"""
Expand Down

0 comments on commit 47be9d2

Please sign in to comment.