将 Pandas DataFrame 与 In-Memory Feather 相互转换

Ram*_*gil 7 python python-3.x pandas feather apache-arrow

使用pandas 中IO 工具可以将 a 转换为DataFrame内存中的羽化缓冲区:

import pandas as pd  
from io import BytesIO 

df = pd.DataFrame({'a': [1,2], 'b': [3.0,4.0]})  

buf = BytesIO()

df.to_feather(buf)
Run Code Online (Sandbox Code Playgroud)

但是,使用相同的缓冲区转换回 DataFrame

pd.read_feather(buf)
Run Code Online (Sandbox Code Playgroud)

结果报错:

ArrowInvalid:不是羽毛文件

如何将 DataFrame 转换为内存中的羽化表示,并相应地转换回 DataFrame?

预先感谢您的考虑和回应。

luk*_*ris 5

有了pandas==0.25.2这个,可以通过以下方式完成:

import pandas
import io
df = pandas.DataFrame(data={'a': [1, 2], 'b': [3.0, 4.0]})
buf = io.BytesIO()
df.to_feather(buf)
output = pandas.read_feather(buf)
Run Code Online (Sandbox Code Playgroud)

然后调用output.head(2)返回:

    a    b
 0  1  3.0
 1  2  4.0
Run Code Online (Sandbox Code Playgroud)

如果您有DataFrame多个索引,您可能会看到类似的错误

ValueError:feather 不支持对索引进行序列化;您可以 .reset_index() 将索引变成列

在这种情况下,你需要调用.reset_index()之前to_feather,并调用.set_index([...])read_feather


我想补充的最后一件事是,如果您正在对 进行某些操作,则BytesIO需要在写入羽化字节后返回到 0。例如:

    a    b
 0  1  3.0
 1  2  4.0
Run Code Online (Sandbox Code Playgroud)