簡體   English   中英

將 Pandas DataFrame 序列化為內存緩沖區表示

[英]Serialize Pandas DataFrame to in-memory buffer representation

將 DataFrame 序列化為內存表示的最快方法是什么? 根據一些研究, Apache Feather格式似乎是大多數指標中最快的可用格式。

我的目標是獲取 DataFrame 的序列化字節 - Feather 的唯一問題是我想避免寫入磁盤和從磁盤加載的開銷,而 Feather API 似乎只允許文件 I/O。 有沒有我應該研究的不同格式,或者 Python 中是否有一種方法可以“偽造”文件,迫使 Feather 改為寫入內存緩沖區?

pyarrow提供BufferOutputStream用於寫入 memory 而不是文件。 與文檔字符串相比, read_featherwrite_feather還支持從 memory 讀取/寫入寫入器接口。

使用以下代碼,您可以將 DataFrame 序列化為 memory 而無需進入文件系統,然后直接重新構建它。

from pyarrow.feather import read_feather, write_feather
import pandas as pd
import pyarrow as pa

df = pd.DataFrame({"column": [1, 2]})
output_stream = pa.BufferOutputStream()
write_feather(df, output_stream)
df_reconstructed = read_feather(output_stream.getvalue())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM