我正在使用
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'
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'))
)