Skip to content

Commit 6ceac2c

Browse files
author
Jay Clifford
committed
HOTFIX: Polars dataframe unix timestamp
1 parent f027046 commit 6ceac2c

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

influxdb_client_3/write_client/client/write/dataframe_serializer.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -352,14 +352,16 @@ def to_line_protocol(self, row):
352352
# add escape symbols for special characters to tags
353353

354354
fields = ",".join(
355-
f"{col}=\"{row[self.column_indices[col]]}\"" if isinstance(row[self.column_indices[col]], str)
355+
f"{col}=\"{self.escape_value(row[self.column_indices[col]])}\"" if isinstance(row[self.column_indices[col]], str)
356+
else f"{col}={str(row[self.column_indices[col]]).lower()}" if isinstance(row[self.column_indices[col]], bool) # Check for bool first
356357
else f"{col}={row[self.column_indices[col]]}i" if isinstance(row[self.column_indices[col]], int)
357358
else f"{col}={row[self.column_indices[col]]}"
358359
for col in self.column_indices
359360
if col not in self.tag_columns + [self.timestamp_column]
360361
and row[self.column_indices[col]] is not None and row[self.column_indices[col]] != ""
361362
)
362363

364+
363365
# Access the Unix timestamp
364366
timestamp = row[self.column_indices[self.timestamp_column]]
365367
if tags != "":
@@ -375,19 +377,22 @@ def serialize(self, chunk_idx: int = None):
375377

376378
df = self.data_frame
377379

378-
# Convert timestamp to unix timestamp
379-
if self.precision is None:
380-
df = df.with_columns(pl.col(self.timestamp_column).dt.epoch(time_unit="ns").alias(self.timestamp_column))
381-
elif self.precision == 'ns':
382-
df = df.with_columns(pl.col(self.timestamp_column).dt.epoch(time_unit="ns").alias(self.timestamp_column))
383-
elif self.precision == 'us':
384-
df = df.with_columns(pl.col(self.timestamp_column).dt.epoch(time_unit="us").alias(self.timestamp_column))
385-
elif self.precision == 'ms':
386-
df = df.with_columns(pl.col(self.timestamp_column).dt.epoch(time_unit="ms").alias(self.timestamp_column))
387-
elif self.precision == 's':
388-
df = df.with_columns(pl.col(self.timestamp_column).dt.epoch(time_unit="s").alias(self.timestamp_column))
380+
# Check if the timestamp column is already an integer
381+
if df[self.timestamp_column].dtype in [pl.Int32, pl.Int64]:
382+
# The timestamp column is already an integer, assuming it's in Unix format
383+
pass
389384
else:
390-
raise ValueError(f"Unsupported precision: {self.precision}")
385+
# Convert timestamp to Unix timestamp based on specified precision
386+
if self.precision in [None, 'ns']:
387+
df = df.with_columns(pl.col(self.timestamp_column).dt.epoch(time_unit="ns").alias(self.timestamp_column))
388+
elif self.precision == 'us':
389+
df = df.with_columns(pl.col(self.timestamp_column).dt.epoch(time_unit="us").alias(self.timestamp_column))
390+
elif self.precision == 'ms':
391+
df = df.with_columns(pl.col(self.timestamp_column).dt.epoch(time_unit="ms").alias(self.timestamp_column))
392+
elif self.precision == 's':
393+
df = df.with_columns(pl.col(self.timestamp_column).dt.epoch(time_unit="s").alias(self.timestamp_column))
394+
else:
395+
raise ValueError(f"Unsupported precision: {self.precision}")
391396

392397
if chunk_idx is None:
393398
chunk = df

0 commit comments

Comments
 (0)