是否可以在ORC文件中转换熊猫数据框?我可以在镶木地板文件中转换df,但是该库似乎不支持ORC。 Python中是否有可用的解决方案?如果没有,最好的策略是什么?一种选择是使用外部工具将镶木地板文件转换为ORC,但我不知道在哪里可以找到它。
答案 0 :(得分:1)
我最近使用了具有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()
答案 1 :(得分:1)
此答案已使用 pyarrow==4.0.1
和 pandas==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
作家。