【问题标题】:Convert Pandas dataframe from/to ORC file将 Pandas 数据帧从 ORC 文件转换为 ORC 文件
【发布时间】:2020-03-02 20:20:41
【问题描述】:

是否可以将 Pandas 数据帧从 ORC 文件转换为 ORC 文件?我可以在 parquet 文件中转换 df,但该库似乎不支持 ORC。 Python中有可用的解决方案吗?如果没有,最好的策略是什么?一种选择是使用外部工具将 parquet 文件转换为 ORC,但我不知道在哪里可以找到它。

【问题讨论】:

  • 您使用的是 Hive 还是 Spark(或两者都使用)?如果你有其中之一,那么做你想做的事情就会容易得多,而且没有错误。特别是,我强烈建议您使用 Hive 来管理您的 ORC 文件。您可以使用 pyodbc 或 pyhive 包在 python 中连接到它。
  • @alcor 我刚刚完成了 C++ 和 Python 中的 ORC 适配器,因此如果您使用我的 fork,现在可以编写 ORC 文件:github.com/mathyingzhou/arrow

标签: python pandas orc


【解决方案1】:

这个答案是用pyarrow==4.0.1pandas==1.2.5 测试的。

它首先使用pyarrow.Table.from_pandas 创建一个pyarrow 表。然后它使用pyarrow.orc.ORCFile 写入orc 文件。

读兽人

import pandas as pd
import pyarrow.orc  # This prevents: AttributeError: module 'pyarrow' has no attribute 'orc'

df = pd.read_orc('/tmp/your_df.orc')

写兽人

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

# Here prepare your pandas df.

table = pa.Table.from_pandas(df, preserve_index=False)
orc.write_table(table, '/tmp/your_df.orc')

截至pandas==1.3.0,还没有pd.to_orc 作家。

【讨论】:

  • 您知道是否可以在使用您描述的解决方案编写 ORC 文件时添加压缩类型?
【解决方案2】:

我最近使用了支持 ORC 的 pyarrow,尽管我看到了一些未加载 pyarrow.orc 模块的问题。

pip install pyarrow

使用:

import pandas as pd
import pyarrow.orc as orc

with open(filename) as file:
    data = orc.ORCFile(file)
    df = data.read().to_pandas()

【讨论】:

    猜你喜欢
    • 2018-07-31
    • 2019-07-01
    • 2022-08-24
    • 2020-05-10
    • 2020-09-21
    • 2019-06-26
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多