Skip to content

Commit a3e18ee

Browse files
authored
docs: fix an example about how to write Pandas.DataFrame (#196)
1 parent d396581 commit a3e18ee

File tree

1 file changed

+65
-60
lines changed

1 file changed

+65
-60
lines changed

README.rst

+65-60
Original file line numberDiff line numberDiff line change
@@ -271,77 +271,82 @@ The batching is configurable by ``write_options``\ :
271271

272272
.. code-block:: python
273273
274-
import rx
275-
from rx import operators as ops
274+
from datetime import datetime, timedelta
276275
277-
from influxdb_client import InfluxDBClient, Point, WriteOptions
278-
from influxdb_client.client.write_api import SYNCHRONOUS
276+
import pandas as pd
277+
import rx
278+
from pytz import UTC
279+
from rx import operators as ops
279280
280-
_client = InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org")
281-
_write_client = _client.write_api(write_options=WriteOptions(batch_size=500,
282-
flush_interval=10_000,
283-
jitter_interval=2_000,
284-
retry_interval=5_000,
285-
max_retries=5,
286-
max_retry_delay=30_000,
287-
exponential_base=2))
281+
from influxdb_client import InfluxDBClient, Point, WriteOptions
288282
289-
"""
290-
Write Line Protocol formatted as string
291-
"""
292-
_write_client.write("my-bucket", "my-org", "h2o_feet,location=coyote_creek water_level=1.0 1")
293-
_write_client.write("my-bucket", "my-org", ["h2o_feet,location=coyote_creek water_level=2.0 2",
294-
"h2o_feet,location=coyote_creek water_level=3.0 3"])
283+
_client = InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org")
284+
_write_client = _client.write_api(write_options=WriteOptions(batch_size=500,
285+
flush_interval=10_000,
286+
jitter_interval=2_000,
287+
retry_interval=5_000,
288+
max_retries=5,
289+
max_retry_delay=30_000,
290+
exponential_base=2))
295291
296-
"""
297-
Write Line Protocol formatted as byte array
298-
"""
299-
_write_client.write("my-bucket", "my-org", "h2o_feet,location=coyote_creek water_level=1.0 1".encode())
300-
_write_client.write("my-bucket", "my-org", ["h2o_feet,location=coyote_creek water_level=2.0 2".encode(),
301-
"h2o_feet,location=coyote_creek water_level=3.0 3".encode()])
292+
"""
293+
Write Line Protocol formatted as string
294+
"""
295+
_write_client.write("my-bucket", "my-org", "h2o_feet,location=coyote_creek water_level=1.0 1")
296+
_write_client.write("my-bucket", "my-org", ["h2o_feet,location=coyote_creek water_level=2.0 2",
297+
"h2o_feet,location=coyote_creek water_level=3.0 3"])
302298
303-
"""
304-
Write Dictionary-style object
305-
"""
306-
_write_client.write("my-bucket", "my-org", {"measurement": "h2o_feet", "tags": {"location": "coyote_creek"},
307-
"fields": {"water_level": 1.0}, "time": 1})
308-
_write_client.write("my-bucket", "my-org", [{"measurement": "h2o_feet", "tags": {"location": "coyote_creek"},
309-
"fields": {"water_level": 2.0}, "time": 2},
310-
{"measurement": "h2o_feet", "tags": {"location": "coyote_creek"},
311-
"fields": {"water_level": 3.0}, "time": 3}])
299+
"""
300+
Write Line Protocol formatted as byte array
301+
"""
302+
_write_client.write("my-bucket", "my-org", "h2o_feet,location=coyote_creek water_level=1.0 1".encode())
303+
_write_client.write("my-bucket", "my-org", ["h2o_feet,location=coyote_creek water_level=2.0 2".encode(),
304+
"h2o_feet,location=coyote_creek water_level=3.0 3".encode()])
312305
313-
"""
314-
Write Data Point
315-
"""
316-
_write_client.write("my-bucket", "my-org", Point("h2o_feet").tag("location", "coyote_creek").field("water_level", 4.0).time(4))
317-
_write_client.write("my-bucket", "my-org", [Point("h2o_feet").tag("location", "coyote_creek").field("water_level", 5.0).time(5),
318-
Point("h2o_feet").tag("location", "coyote_creek").field("water_level", 6.0).time(6)])
306+
"""
307+
Write Dictionary-style object
308+
"""
309+
_write_client.write("my-bucket", "my-org", {"measurement": "h2o_feet", "tags": {"location": "coyote_creek"},
310+
"fields": {"water_level": 1.0}, "time": 1})
311+
_write_client.write("my-bucket", "my-org", [{"measurement": "h2o_feet", "tags": {"location": "coyote_creek"},
312+
"fields": {"water_level": 2.0}, "time": 2},
313+
{"measurement": "h2o_feet", "tags": {"location": "coyote_creek"},
314+
"fields": {"water_level": 3.0}, "time": 3}])
319315
320-
"""
321-
Write Observable stream
322-
"""
323-
_data = rx \
324-
.range(7, 11) \
325-
.pipe(ops.map(lambda i: "h2o_feet,location=coyote_creek water_level={0}.0 {0}".format(i)))
316+
"""
317+
Write Data Point
318+
"""
319+
_write_client.write("my-bucket", "my-org",
320+
Point("h2o_feet").tag("location", "coyote_creek").field("water_level", 4.0).time(4))
321+
_write_client.write("my-bucket", "my-org",
322+
[Point("h2o_feet").tag("location", "coyote_creek").field("water_level", 5.0).time(5),
323+
Point("h2o_feet").tag("location", "coyote_creek").field("water_level", 6.0).time(6)])
326324
327-
_write_client.write("my-bucket", "my-org", _data)
325+
"""
326+
Write Observable stream
327+
"""
328+
_data = rx \
329+
.range(7, 11) \
330+
.pipe(ops.map(lambda i: "h2o_feet,location=coyote_creek water_level={0}.0 {0}".format(i)))
328331
329-
"""
330-
Write Pandas DataFrame
331-
"""
332-
_now = pd.Timestamp().now('UTC')
333-
_data_frame = pd.DataFrame(data=[["coyote_creek", 1.0], ["coyote_creek", 2.0]],
334-
index=[now, now + timedelta(hours=1)],
335-
columns=["location", "water_level"])
332+
_write_client.write("my-bucket", "my-org", _data)
336333
337-
_write_client.write(bucket.name, record=data_frame, data_frame_measurement_name='h2o_feet',
338-
data_frame_tag_columns=['location'])
334+
"""
335+
Write Pandas DataFrame
336+
"""
337+
_now = datetime.now(UTC)
338+
_data_frame = pd.DataFrame(data=[["coyote_creek", 1.0], ["coyote_creek", 2.0]],
339+
index=[_now, _now + timedelta(hours=1)],
340+
columns=["location", "water_level"])
339341
340-
"""
341-
Close client
342-
"""
343-
_write_client.__del__()
344-
_client.__del__()
342+
_write_client.write("my-bucket", "my-org", record=_data_frame, data_frame_measurement_name='h2o_feet',
343+
data_frame_tag_columns=['location'])
344+
345+
"""
346+
Close client
347+
"""
348+
_write_client.__del__()
349+
_client.__del__()
345350
346351
.. marker-batching-end
347352

0 commit comments

Comments
 (0)