如何将浮点数转换为Parquet TIMESTAMP逻辑类型?

问题描述

假设我有一个pyarrow表,其中的列 <RollingRandomAccessFile name="STD_LOG" filePattern="logs/kunal.%d{yyyy-MM-dd}.log" immediateFlush="true"> <PatternLayout pattern="%d{ISO8601} %-5p %t: [%c] [UNIQUE_ID:%X{UNIQUE_ID}] %m%n" /> <Policies> <crontriggeringPolicy schedule="0 0 0 ? * *" evaluateOnStartup="true"/> </Policies> 包含Timestamp。 这些浮点数实际上是以s为单位的时间戳。 例如:

float64

我从documentation阅读了有关镶木地板逻辑类型的信息。 请如何将这些浮点值转换为逻辑类型TIMESTAMP? 我没有有关此操作方法的文档。

感谢您的帮助。 祝你有美好的一天, 最好的

解决方法

您需要将浮点数转换为pyarrow中的实际时间戳类型,然后将其自动写入paruet逻辑时间戳类型。

使用pyarrow.compute模块,此转换也可以在pyarrow中完成(与在熊猫中进行转换相比,在人体工程学上要少一些,但避免了向熊猫和熊猫的转换):

>>> import pyarrow.compute as pc
>>> arr = pa.array([1600419000.477,1600419001.027])
>>> pc.multiply(arr,pa.scalar(1000.)).cast("int64").cast(pa.timestamp('ms'))
<pyarrow.lib.TimestampArray object at 0x7fe5ec3df588>
[
  2020-09-18 08:50:00.477,2020-09-18 08:50:01.027
]
,

我认为您将无法在箭头内将浮点数转换为时间戳记。

箭头假定时间戳是给定精度(ms,us,ns)的64位整数。在您的情况下,您必须将秒浮点数乘以所需的精度(1000表示毫秒),然后转换为int64并转换为时间戳。

这是使用熊猫的示例:

(
    pa.array([1600419000.477,1600419001.027])
    .to_pandas()
    .mul(1000)
    .astype('long')
    .pipe(pa.Array.from_pandas)
    .cast(pa.timestamp('ms'))
)

哪个给你:

<pyarrow.lib.TimestampArray object at 0x7fb5025b6a08>
[
  2020-09-18 08:50:00.477,2020-09-18 08:50:01.027
]