将 Pandas 数据帧与 ORC 文件相互转换

alc*_*cor 8 python pandas orc

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

Acu*_*nus 7

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

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

读兽人

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

df = pd.read_orc('/tmp/your_df.orc')
Run Code Online (Sandbox Code Playgroud)

写兽人

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')
Run Code Online (Sandbox Code Playgroud)

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


Phy*_*hy6 0

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

pip install pyarrow
Run Code Online (Sandbox Code Playgroud)

使用:

import pandas as pd
import pyarrow.orc as orc

with open(filename) as file:
    data = orc.ORCFile(file)
    df = data.read().to_pandas()
Run Code Online (Sandbox Code Playgroud)