【问题标题】:Error converting pandas dataframe to ORC using pyarrow使用 pyarrow 将 pandas 数据帧转换为 ORC 时出错
【发布时间】:2022-08-24 19:01:36
【问题描述】:

我正在尝试使用 Pyarrow 将 Pandas DataFrame 保存为 .orc 文件。包的版本是:pandas==1.3.5pyarrow==6.0.1。我的 python3 版本是3.9.12

这是代码sn-p:

import pandas as pd
import pyarrow as pa
import pyarrow.orc as orc

df = pd.read_orc(\'sample.orc\')
table = pa.Table.from_pandas(df, preserve_index=False)
orc.write_table(table, \'sample_rewritten.orc\')

我得到的错误是:ArrowNotImplementedError: Unknown or unsupported Arrow type: null

如何在 python 中将 Pandas DataFrame (csv) 保存为 .orc 文件?

write_table 行失败。 这是整个堆栈跟踪:

ArrowNotImplementedError                  Traceback (most recent call last)
Input In [1], in <cell line: 7>()
      5 df = pd.read_orc(\'hats_v2_sample.orc\')
      6 table = pa.Table.from_pandas(df, preserve_index=False)
----> 7 orc.write_table(table, \'sample_rewritten.orc\')

File /opt/homebrew/lib/python3.9/site-packages/pyarrow/orc.py:176, in write_table(table, where)
    174     table, where = where, table
    175 writer = ORCWriter(where)
--> 176 writer.write(table)
    177 writer.close()

File /opt/homebrew/lib/python3.9/site-packages/pyarrow/orc.py:146, in ORCWriter.write(self, table)
    136 def write(self, table):
    137     \"\"\"
    138     Write the table into an ORC file. The schema of the table must
    139     be equal to the schema used when opening the ORC file.
   (...)
    144         The table to be written into the ORC file
    145     \"\"\"
--> 146     self.writer.write(table)

File /opt/homebrew/lib/python3.9/site-packages/pyarrow/_orc.pyx:159, in pyarrow._orc.ORCWriter.write()

File /opt/homebrew/lib/python3.9/site-packages/pyarrow/error.pxi:120, in pyarrow.lib.check_status()

ArrowNotImplementedError: Unknown or unsupported Arrow type: null
  • 哪条线到底失败了? from_pandas 还是 write_table?你能提供完整的堆栈跟踪吗?
  • 我已经用整个堆栈跟踪更新了这个问题
  • 看起来您的源表有一个pa.null() 类型的列(这意味着它没有任何数据)。看起来兽人也不支持空列。您需要找出导致问题的列以及原因。为此,您可以致电print(table.schema),它会告诉您每列的类型。
  • 好的,谢谢。

标签: python-3.x pandas pyarrow orc


【解决方案1】:

请注意是否可以等待,但 Pandas v1.5.0(即将发布)将原生支持写入 ORC 文件。

https://github.com/pandas-dev/pandas/pull/44554

【讨论】:

  • 谢谢提供信息。这是否允许将具有空列的 pandas DataFrames 转换为 orc 文件?
【解决方案2】:

当您在数据框中导出具有空值的数据时,会出现此问题。 您可以使用 df.fillna(value = 0,inplace = True) 然后将数据框导出到orc文件

【讨论】:

    猜你喜欢
    • 2020-03-02
    • 2021-05-29
    • 2021-08-31
    • 2016-09-27
    • 2014-04-15
    • 2022-08-16
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多