读取以纪元时间戳列作为时间戳的 CSV

问题描述 投票:0回答:1

我正在使用

pyarrow.csv
读取 CSV 文件并将其转换为镶木地板。此 CSV 文件有一个
timestamp
列,其中包含代表 Unix 时间的 int。

尽管如此,它会将其读取为 int64,如果我尝试使用

convertoptions
,则会引发错误:

import pyarrow as pa
import pyarrow.csv as pv

table = pv.read_csv("file.csv", convert_options=pv.ConvertOptions(
    column_types={
        'timestamp': pa.timestamp('s'),
    }
))

这会引发以下错误:

ArrowInvalid: In CSV column #1: CSV conversion error to timestamp[s]: invalid value '1705173443'
python csv pyarrow
1个回答
0
投票

虽然我还没有找到直接从 CSV 读取它的方法,但我可以使用

pyarrow.compute.cast
将整数转换为时间戳:

table = table.set_column(
  # Put the new column in the same place than the old one
  table.column_names.index('timestamp'), 
  # Keep the same name
  'timestamp', 
  pc.cast(table['timestamp'], pa.timestamp('s'))
)
© www.soinside.com 2019 - 2024. All rights reserved.