Skip to content

Commit 9b20924

Browse files
authored
feat: add supports for custom precision for index specified as number [DataFrame] (#335)
1 parent 76bdb62 commit 9b20924

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
## 1.22.0 [unreleased]
22

33
### Features
4-
1. [#330](https://github.com/influxdata/influxdb-client-python/pull/330): Add supports for write structured data - `NamedTuple`, `Data Classes`
4+
1. [#330](https://github.com/influxdata/influxdb-client-python/pull/330): Add support for write structured data - `NamedTuple`, `Data Classes`
5+
1. [#335](https://github.com/influxdata/influxdb-client-python/pull/335): Add support for custom precision for index specified as number [DataFrame]
56

67
### Documentation
78
1. [#331](https://github.com/influxdata/influxdb-client-python/pull/331): Add [Migration Guide](MIGRATION_GUIDE.rst)

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def write(self, bucket: str, org: str = None,
252252
:param WritePrecision write_precision: specifies the precision for the unix timestamps within
253253
the body line-protocol. The precision specified on a Point has precedes
254254
and is use for write.
255-
:param record: Point, Line Protocol, Dictionary, NamedTuple, Data Classes, Pandas DataFrame or
255+
:param record: Point, Line Protocol, Dictionary, NamedTuple, Data Classes, Pandas DataFrame or
256256
RxPY Observable to write
257257
:key data_frame_measurement_name: name of measurement for writing Pandas DataFrame - ``DataFrame``
258258
:key data_frame_tag_columns: list of DataFrame columns which are tags,
@@ -284,7 +284,26 @@ def write(self, bucket: str, org: str = None,
284284
point = Point("h2o_feet").tag("location", "us-west").field("level", 125).time(1)
285285
write_api.write("my-bucket", "my-org", point)
286286
287-
"""
287+
DataFrame:
288+
The index of `Pandas DataFrame <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html>`_
289+
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>`_
290+
or its must be transformable to ``datetime`` by
291+
`pandas.to_datetime <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html#pandas.to_datetime>`_.
292+
293+
If you would like to transform a column to ``PeriodIndex``, you can use something like:
294+
295+
.. code-block:: python
296+
297+
import pandas as pd
298+
299+
# DataFrame
300+
data_frame = ...
301+
# Set column as Index
302+
data_frame.set_index('column_name', inplace=True)
303+
# Transform index to PeriodIndex
304+
data_frame.index = pd.to_datetime(data_frame.index, unit='s')
305+
306+
""" # noqa: E501
288307
org = get_org_query_param(org=org, client=self._influxdb_client)
289308

290309
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)