【发布时间】:2021-05-21 21:06:22
【问题描述】:
我在 pandas 中有一个数据框,我想使用 pyarrow 将其写成镶木地板。
我还需要能够指定列类型。如果我通过 pandas 更改类型,则不会出错;但是当我通过 pyarrow 更改它时,出现错误。查看示例:
给定
import pandas as pd
import pyarrow as pa
data = {"col": [86002575]}
df = pd.DataFrame(data)
通过熊猫
df = df.astype({"col": "float32"})
table = pa.Table.from_pandas(df)
没有错误
通过 PyArrow
schema = pa.Schema.from_pandas(df)
i = schema.get_field_index("col")
schema = schema.set(i, pa.field("col", pa.float32()))
table = pa.Table.from_pandas(df, schema=schema)
得到错误:
pyarrow.lib.ArrowInvalid: ('Integer value 86002575 not in range: -16777216 to 16777216', 'Conversion failed for column col with type int64')
我什至不认识那个范围。是不是在两者之间转换的时候尝试做一些中间转换?
【问题讨论】:
标签: python pandas dataframe pyarrow