Skip to content

Commit e9326b5

Browse files
committed
docs: add documentation about supported str formats
1 parent 486fa49 commit e9326b5

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

influxdb_client/client/write/dataframe_serializer.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
4141
:param chunk_size: The size of chunk for serializing into chunks.
4242
:key data_frame_measurement_name: name of measurement for writing Pandas DataFrame
4343
:key data_frame_tag_columns: list of DataFrame columns which are tags, rest columns will be fields
44-
:key data_frame_timestamp_column: DataFrame column which contains timestamp
45-
"""
44+
:key data_frame_timestamp_column: name of DataFrame column which contains a timestamp. The column can be defined as a :class:`~str` value
45+
formatted as `2018-10-26`, `2018-10-26 12:00`, `2018-10-26 12:00:00-05:00`
46+
or other formats and types supported by `pandas.to_datetime <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html#pandas.to_datetime>`_ - ``DataFrame``
47+
""" # noqa: E501
4648
# This function is hard to understand but for good reason:
4749
# the approach used here is considerably more efficient
4850
# than the alternatives.
@@ -279,6 +281,8 @@ def data_frame_to_list_of_points(data_frame, point_settings, precision=DEFAULT_W
279281
:param precision: The precision for the unix timestamps within the body line-protocol.
280282
:key data_frame_measurement_name: name of measurement for writing Pandas DataFrame
281283
:key data_frame_tag_columns: list of DataFrame columns which are tags, rest columns will be fields
282-
:key data_frame_timestamp_column: DataFrame column which contains timestamps
283-
"""
284+
:key data_frame_timestamp_column: name of DataFrame column which contains a timestamp. The column can be defined as a :class:`~str` value
285+
formatted as `2018-10-26`, `2018-10-26 12:00`, `2018-10-26 12:00:00-05:00`
286+
or other formats and types supported by `pandas.to_datetime <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html#pandas.to_datetime>`_ - ``DataFrame``
287+
""" # noqa: E501
284288
return DataframeSerializer(data_frame, point_settings, precision, **kwargs).serialize()

influxdb_client/client/write_api.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,9 @@ def write(self, bucket: str, org: str = None,
294294
:key data_frame_measurement_name: name of measurement for writing Pandas DataFrame - ``DataFrame``
295295
:key data_frame_tag_columns: list of DataFrame columns which are tags,
296296
rest columns will be fields - ``DataFrame``
297-
:key data_frame_timestamp_column: DataFrame column which contains timestamp - ``DataFrame``
297+
:key data_frame_timestamp_column: name of DataFrame column which contains a timestamp. The column can be defined as a :class:`~str` value
298+
formatted as `2018-10-26`, `2018-10-26 12:00`, `2018-10-26 12:00:00-05:00`
299+
or other formats and types supported by `pandas.to_datetime <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html#pandas.to_datetime>`_ - ``DataFrame``
298300
:key record_measurement_key: key of record with specified measurement -
299301
``dictionary``, ``NamedTuple``, ``dataclass``
300302
:key record_measurement_name: static measurement name - ``dictionary``, ``NamedTuple``, ``dataclass``
@@ -323,8 +325,8 @@ def write(self, bucket: str, org: str = None,
323325
write_api.write("my-bucket", "my-org", point)
324326
325327
DataFrame:
326-
The index of `Pandas DataFrame <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html>`_
327-
is used as a ``timestamp`` for written data. The index should be `PeriodIndex <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.PeriodIndex.html#pandas.PeriodIndex>`_
328+
If the ``data_frame_timestamp_column`` is not specified the index of `Pandas DataFrame <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html>`_
329+
is used as a ``timestamp`` for written data. The index can be `PeriodIndex <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.PeriodIndex.html#pandas.PeriodIndex>`_
328330
or its must be transformable to ``datetime`` by
329331
`pandas.to_datetime <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html#pandas.to_datetime>`_.
330332

influxdb_client/client/write_api_async.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ async def write(self, bucket: str, org: str = None,
5858
:key data_frame_measurement_name: name of measurement for writing Pandas DataFrame - ``DataFrame``
5959
:key data_frame_tag_columns: list of DataFrame columns which are tags,
6060
rest columns will be fields - ``DataFrame``
61-
:key data_frame_timestamp_column: DataFrame column which contains timestamp - ``DataFrame``
61+
:key data_frame_timestamp_column: name of DataFrame column which contains a timestamp. The column can be defined as a :class:`~str` value
62+
formatted as `2018-10-26`, `2018-10-26 12:00`, `2018-10-26 12:00:00-05:00`
63+
or other formats and types supported by `pandas.to_datetime <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html#pandas.to_datetime>`_ - ``DataFrame``
6264
:key record_measurement_key: key of record with specified measurement -
6365
``dictionary``, ``NamedTuple``, ``dataclass``
6466
:key record_measurement_name: static measurement name - ``dictionary``, ``NamedTuple``, ``dataclass``
@@ -88,8 +90,8 @@ async def write(self, bucket: str, org: str = None,
8890
await write_api.write("my-bucket", "my-org", point)
8991
9092
DataFrame:
91-
The index of `Pandas DataFrame <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html>`_
92-
is used as a ``timestamp`` for written data. The index should be `PeriodIndex <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.PeriodIndex.html#pandas.PeriodIndex>`_
93+
If the ``data_frame_timestamp_column`` is not specified the index of `Pandas DataFrame <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html>`_
94+
is used as a ``timestamp`` for written data. The index can be `PeriodIndex <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.PeriodIndex.html#pandas.PeriodIndex>`_
9395
or its must be transformable to ``datetime`` by
9496
`pandas.to_datetime <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html#pandas.to_datetime>`_.
9597

tests/test_WriteApiDataFrame.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,32 @@ def test_use_timestamp_from_specified_column(self):
433433
self.assertEqual('test value1=10i,value2=30i 1586044800000000000', points[0])
434434
self.assertEqual('test value1=20i,value2=40i 1588636800000000000', points[1])
435435

436+
def test_str_format_for_timestamp(self):
437+
from influxdb_client.extras import pd
438+
439+
time_formats = [
440+
('2018-10-26', 'test value1=10i,value2=20i 1540512000000000000'),
441+
('2018-10-26 10:00', 'test value1=10i,value2=20i 1540548000000000000'),
442+
('2018-10-26 10:00:00-05:00', 'test value1=10i,value2=20i 1540566000000000000'),
443+
('2018-10-26T11:00:00+00:00', 'test value1=10i,value2=20i 1540551600000000000'),
444+
('2018-10-26 12:00:00+00:00', 'test value1=10i,value2=20i 1540555200000000000'),
445+
('2018-10-26T16:00:00-01:00', 'test value1=10i,value2=20i 1540573200000000000'),
446+
]
447+
448+
for time_format in time_formats:
449+
data_frame = pd.DataFrame(data={
450+
'column_time': [time_format[0]],
451+
'value1': [10],
452+
'value2': [20],
453+
}, index=['A'])
454+
points = data_frame_to_list_of_points(data_frame=data_frame,
455+
data_frame_measurement_name="test",
456+
data_frame_timestamp_column="column_time",
457+
point_settings=PointSettings())
458+
459+
self.assertEqual(1, len(points))
460+
self.assertEqual(time_format[1], points[0])
461+
436462

437463
class DataSerializerChunksTest(unittest.TestCase):
438464
def test_chunks(self):

0 commit comments

Comments
 (0)