将架构分配给 pa.Table.from_pandas()

Assign schema to pa.Table.from_pandas()

我在使用 pyArrow 将 pandas.DF 转换为镶木地板时遇到此错误:

ArrowInvalid('Error converting from Python objects to Int64: Got Python object of type str but can only handle these types: integer

为了找出问题所在的列,我在 for 循环中创建了一个新的 df,首先使用第一列,然后为每个循环添加另一列。我意识到错误是在 dtype: object 以 0s 开头的列中,我想这就是为什么 pyArrow 想要将列转换为 int 但失败的原因,因为其他值是 UUID

我正在尝试传递架构:(不确定这是否可行)

table = pa.Table.from_pandas(df, schema=schema, preserve_index=False)

架构是:df.dtypes

卡洛斯,您是否尝试过将列转换为此处列出的 pandas 类型之一 https://arrow.apache.org/docs/python/pandas.html

你能post df.dtypes 的输出吗?

如果更改 pandas 列类型没有帮助,您可以定义要传入的 pyarrow 架构。

fields = [
    pa.field('id', pa.int64()),
    pa.field('secondaryid', pa.int64()),
    pa.field('date', pa.timestamp('ms')),
]

my_schema = pa.schema(fields)

table = pa.Table.from_pandas(sample_df, schema=my_schema, preserve_index=False)

更多信息在这里:

https://arrow.apache.org/docs/python/data.html https://arrow.apache.org/docs/python/generated/pyarrow.Table.html#pyarrow.Table.from_pandas https://arrow.apache.org/docs/python/generated/pyarrow.schema.html