Skip to content

Commit b46b26b

Browse files
committed
feat: add supports for custom precision for index specified as number [DataFrame]
1 parent 96d99de commit b46b26b

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

influxdb_client/client/write/dataframe_serializer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
101101
# Instead, it would probably be better to leave
102102
# out the timestamp unless a time column is explicitly
103103
# enabled.
104-
data_frame.index = pd.to_datetime(data_frame.index)
104+
data_frame.index = pd.to_datetime(data_frame.index, unit=precision)
105105

106106
if data_frame.index.tzinfo is None:
107107
data_frame.index = data_frame.index.tz_localize('UTC')

influxdb_client/client/write_api.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,27 @@ def write(self, bucket: str, org: str = None,
232232
:param record: Points, line protocol, Pandas DataFrame, RxPY Observable to write
233233
:key data_frame_measurement_name: name of measurement for writing Pandas DataFrame
234234
:key data_frame_tag_columns: list of DataFrame columns which are tags, rest columns will be fields
235-
"""
235+
236+
DataFrame:
237+
The index of `Pandas DataFrame <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html>`_
238+
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>`_
239+
or its must be transformable to ``datetime`` by
240+
`pandas.to_datetime <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html#pandas.to_datetime>`_.
241+
242+
If you would like to transform a column to ``PeriodIndex``, you can use something like:
243+
244+
.. code-block:: python
245+
246+
import pandas as pd
247+
248+
# DataFrame
249+
data_frame = ...
250+
# Set column as Index
251+
data_frame.set_index('column_name', inplace=True)
252+
# Transform index to PeriodIndex
253+
data_frame.index = pd.to_datetime(data_frame.index, unit='s')
254+
255+
""" # noqa: E501
236256
org = get_org_query_param(org=org, client=self._influxdb_client)
237257

238258
if self._point_settings.defaultTags and record is not None:

tests/test_WriteApiDataFrame.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,26 @@ def test_write_precision(self):
354354
self.assertEqual(1, len(points))
355355
self.assertEqual(f"h2o level=15i {precision[1]}", points[0])
356356

357+
def test_index_not_periodIndex_respect_write_precision(self):
358+
from influxdb_client.extras import pd
359+
360+
precisions = [
361+
(WritePrecision.NS, 1586044800000000000),
362+
(WritePrecision.US, 1586044800000000),
363+
(WritePrecision.MS, 1586044800000),
364+
(WritePrecision.S, 1586044800),
365+
(None, 1586044800000000000)
366+
]
367+
368+
for precision in precisions:
369+
data_frame = pd.DataFrame([15], index=[precision[1]], columns=['level'])
370+
points = data_frame_to_list_of_points(data_frame=data_frame,
371+
data_frame_measurement_name='h2o',
372+
point_settings=PointSettings(),
373+
precision=precision[0])
374+
self.assertEqual(1, len(points))
375+
self.assertEqual(f"h2o level=15i {precision[1]}", points[0])
376+
357377

358378
class DataSerializerChunksTest(unittest.TestCase):
359379
def test_chunks(self):

0 commit comments

Comments
 (0)