Skip to content

Commit 224d745

Browse files
committed
docs: add possibility to specify timestamp column
1 parent e9326b5 commit 224d745

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

influxdb_client/client/write/dataframe_serializer.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
4444
:key data_frame_timestamp_column: name of DataFrame column which contains a timestamp. The column can be defined as a :class:`~str` value
4545
formatted as `2018-10-26`, `2018-10-26 12:00`, `2018-10-26 12:00:00-05:00`
4646
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+
:key data_frame_timestamp_timezone: name of the timezone which is used for timestamp column - ``DataFrame``
4748
""" # noqa: E501
4849
# This function is hard to understand but for good reason:
4950
# the approach used here is considerably more efficient
@@ -96,6 +97,7 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
9697
raise TypeError('"data_frame_measurement_name" is a Required Argument')
9798

9899
timestamp_column = kwargs.get('data_frame_timestamp_column', None)
100+
timestamp_timezone = kwargs.get('data_frame_timestamp_timezone', None)
99101
data_frame = data_frame.copy(deep=False)
100102
data_frame_timestamp = data_frame.index if timestamp_column is None else data_frame[timestamp_column]
101103
if isinstance(data_frame_timestamp, pd.PeriodIndex):
@@ -108,6 +110,12 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
108110
# enabled.
109111
data_frame_timestamp = pd.to_datetime(data_frame_timestamp, unit=precision)
110112

113+
if timestamp_timezone:
114+
if isinstance(data_frame_timestamp, pd.DatetimeIndex):
115+
data_frame_timestamp = data_frame_timestamp.tz_localize(timestamp_timezone)
116+
else:
117+
data_frame_timestamp = data_frame_timestamp.dt.tz_localize(timestamp_timezone)
118+
111119
if hasattr(data_frame_timestamp, 'tzinfo') and data_frame_timestamp.tzinfo is None:
112120
data_frame_timestamp = data_frame_timestamp.tz_localize('UTC')
113121
if timestamp_column is None:
@@ -284,5 +292,6 @@ def data_frame_to_list_of_points(data_frame, point_settings, precision=DEFAULT_W
284292
:key data_frame_timestamp_column: name of DataFrame column which contains a timestamp. The column can be defined as a :class:`~str` value
285293
formatted as `2018-10-26`, `2018-10-26 12:00`, `2018-10-26 12:00:00-05:00`
286294
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``
295+
:key data_frame_timestamp_timezone: name of the timezone which is used for timestamp column - ``DataFrame``
287296
""" # noqa: E501
288297
return DataframeSerializer(data_frame, point_settings, precision, **kwargs).serialize()

influxdb_client/client/write_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ def write(self, bucket: str, org: str = None,
297297
:key data_frame_timestamp_column: name of DataFrame column which contains a timestamp. The column can be defined as a :class:`~str` value
298298
formatted as `2018-10-26`, `2018-10-26 12:00`, `2018-10-26 12:00:00-05:00`
299299
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``
300+
:key data_frame_timestamp_timezone: name of the timezone which is used for timestamp column - ``DataFrame``
300301
:key record_measurement_key: key of record with specified measurement -
301302
``dictionary``, ``NamedTuple``, ``dataclass``
302303
:key record_measurement_name: static measurement name - ``dictionary``, ``NamedTuple``, ``dataclass``

influxdb_client/client/write_api_async.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ async def write(self, bucket: str, org: str = None,
6161
:key data_frame_timestamp_column: name of DataFrame column which contains a timestamp. The column can be defined as a :class:`~str` value
6262
formatted as `2018-10-26`, `2018-10-26 12:00`, `2018-10-26 12:00:00-05:00`
6363
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``
64+
:key data_frame_timestamp_timezone: name of the timezone which is used for timestamp column - ``DataFrame``
6465
:key record_measurement_key: key of record with specified measurement -
6566
``dictionary``, ``NamedTuple``, ``dataclass``
6667
:key record_measurement_name: static measurement name - ``dictionary``, ``NamedTuple``, ``dataclass``

tests/test_WriteApiDataFrame.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,58 @@ def test_str_format_for_timestamp(self):
459459
self.assertEqual(1, len(points))
460460
self.assertEqual(time_format[1], points[0])
461461

462+
def test_specify_timezone(self):
463+
from influxdb_client.extras import pd
464+
data_frame = pd.DataFrame(data={
465+
'column_time': ['2020-05-24 10:00', '2020-05-24 01:00'],
466+
'value1': [10, 20],
467+
'value2': [30, 40],
468+
}, index=['A', 'B'])
469+
470+
points = data_frame_to_list_of_points(data_frame=data_frame,
471+
data_frame_measurement_name="test",
472+
data_frame_timestamp_column="column_time",
473+
data_frame_timestamp_timezone="Europe/Berlin",
474+
point_settings=PointSettings())
475+
476+
self.assertEqual(2, len(points))
477+
self.assertEqual('test value1=10i,value2=30i 1590307200000000000', points[0])
478+
self.assertEqual('test value1=20i,value2=40i 1590274800000000000', points[1])
479+
480+
def test_specify_timezone_date_time_index(self):
481+
from influxdb_client.extras import pd
482+
data_frame = pd.DataFrame(data={
483+
'value1': [10, 20],
484+
'value2': [30, 40],
485+
}, index=[pd.Timestamp('2020-05-24 10:00'), pd.Timestamp('2020-05-24 01:00')])
486+
487+
points = data_frame_to_list_of_points(data_frame=data_frame,
488+
data_frame_measurement_name="test",
489+
data_frame_timestamp_timezone="Europe/Berlin",
490+
point_settings=PointSettings())
491+
492+
self.assertEqual(2, len(points))
493+
self.assertEqual('test value1=10i,value2=30i 1590307200000000000', points[0])
494+
self.assertEqual('test value1=20i,value2=40i 1590274800000000000', points[1])
495+
496+
def test_specify_timezone_period_time_index(self):
497+
from influxdb_client.extras import pd
498+
data_frame = pd.DataFrame(data={
499+
'value1': [10, 20],
500+
'value2': [30, 40],
501+
}, index=pd.period_range(start='2020-05-24 10:00', freq='H', periods=2))
502+
503+
print(data_frame.to_string())
504+
505+
points = data_frame_to_list_of_points(data_frame=data_frame,
506+
data_frame_measurement_name="test",
507+
data_frame_timestamp_timezone="Europe/Berlin",
508+
point_settings=PointSettings())
509+
510+
self.assertEqual(2, len(points))
511+
self.assertEqual('test value1=10i,value2=30i 1590307200000000000', points[0])
512+
self.assertEqual('test value1=20i,value2=40i 1590310800000000000', points[1])
513+
462514

463515
class DataSerializerChunksTest(unittest.TestCase):
464516
def test_chunks(self):

0 commit comments

Comments
 (0)