@@ -41,6 +41,7 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
41
41
:param chunk_size: The size of chunk for serializing into chunks.
42
42
:key data_frame_measurement_name: name of measurement for writing Pandas DataFrame
43
43
: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
44
45
"""
45
46
# This function is hard to understand but for good reason:
46
47
# the approach used here is considerably more efficient
@@ -92,19 +93,25 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
92
93
if data_frame_measurement_name is None :
93
94
raise TypeError ('"data_frame_measurement_name" is a Required Argument' )
94
95
96
+ timestamp_column = kwargs .get ('data_frame_timestamp_column' , None )
95
97
data_frame = data_frame .copy (deep = False )
96
- if isinstance (data_frame .index , pd .PeriodIndex ):
97
- data_frame .index = data_frame .index .to_timestamp ()
98
+ data_frame_timestamp = data_frame .index if timestamp_column is None else data_frame [timestamp_column ]
99
+ if isinstance (data_frame_timestamp , pd .PeriodIndex ):
100
+ data_frame_timestamp = data_frame_timestamp .to_timestamp ()
98
101
else :
99
102
# TODO: this is almost certainly not what you want
100
103
# when the index is the default RangeIndex.
101
104
# Instead, it would probably be better to leave
102
105
# out the timestamp unless a time column is explicitly
103
106
# enabled.
104
- data_frame . index = pd .to_datetime (data_frame . index , unit = precision )
107
+ data_frame_timestamp = pd .to_datetime (data_frame_timestamp , unit = precision )
105
108
106
- if data_frame .index .tzinfo is None :
107
- data_frame .index = data_frame .index .tz_localize ('UTC' )
109
+ if hasattr (data_frame_timestamp , 'tzinfo' ) and data_frame_timestamp .tzinfo is None :
110
+ data_frame_timestamp = data_frame_timestamp .tz_localize ('UTC' )
111
+ if timestamp_column is None :
112
+ data_frame .index = data_frame_timestamp
113
+ else :
114
+ data_frame [timestamp_column ] = data_frame_timestamp
108
115
109
116
data_frame_tag_columns = kwargs .get ('data_frame_tag_columns' )
110
117
data_frame_tag_columns = set (data_frame_tag_columns or [])
@@ -141,6 +148,7 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
141
148
# null_columns has a bool value for each column holding
142
149
# whether that column contains any null (NaN or None) values.
143
150
null_columns = data_frame .isnull ().any ()
151
+ timestamp_index = 0
144
152
145
153
# Iterate through the columns building up the expression for each column.
146
154
for index , (key , value ) in columns :
@@ -164,6 +172,9 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
164
172
key_value = f',{ key_format } ={{str({ val_format } ).translate(_ESCAPE_KEY)}}'
165
173
tags .append (key_value )
166
174
continue
175
+ elif timestamp_column is not None and key in timestamp_column :
176
+ timestamp_index = field_index
177
+ continue
167
178
168
179
# This column is a field column.
169
180
# Note: no comma separator is needed for the first field.
@@ -195,13 +206,13 @@ def __init__(self, data_frame, point_settings, precision=DEFAULT_WRITE_PRECISION
195
206
196
207
tags = '' .join (tags )
197
208
fields = '' .join (fields )
198
- timestamp = '{p[0 ].value}'
209
+ timestamp = '{p[%s ].value}' % timestamp_index
199
210
if precision == WritePrecision .US :
200
- timestamp = '{int(p[0 ].value / 1e3)}'
211
+ timestamp = '{int(p[%s ].value / 1e3)}' % timestamp_index
201
212
elif precision == WritePrecision .MS :
202
- timestamp = '{int(p[0 ].value / 1e6)}'
213
+ timestamp = '{int(p[%s ].value / 1e6)}' % timestamp_index
203
214
elif precision == WritePrecision .S :
204
- timestamp = '{int(p[0 ].value / 1e9)}'
215
+ timestamp = '{int(p[%s ].value / 1e9)}' % timestamp_index
205
216
206
217
f = eval (f'lambda p: f"""{{measurement_name}}{ tags } { fields } { timestamp } """' , {
207
218
'measurement_name' : measurement_name ,
@@ -268,5 +279,6 @@ def data_frame_to_list_of_points(data_frame, point_settings, precision=DEFAULT_W
268
279
:param precision: The precision for the unix timestamps within the body line-protocol.
269
280
:key data_frame_measurement_name: name of measurement for writing Pandas DataFrame
270
281
: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
271
283
"""
272
284
return DataframeSerializer (data_frame , point_settings , precision , ** kwargs ).serialize ()
0 commit comments