具有混合列类型的 Pandas DataFrame 的带外(零拷贝)酸洗

发布于 2025-01-17 01:37:34 字数 1257 浏览 4 评论 0原文

我正在尝试在从单独的控制台会话启动的进程之间实现 Pandas DataFrame 的零拷贝共享。请考虑以下两个Python文件:

生产者.py

import pandas as pd 
import numpy as np 
import pickle

df = pd.DataFrame({'text': ['a','b','c'], 'ints':[1,2,3], 'floats': [1.0,2,3]})


print(df)
#   text  ints  floats
# 0    a     1     1.0
# 1    b     2     2.0
# 2    c     3     3.0

# prints as expected!


buffers = []

with open("my_df.pickle", "wb") as f:
    pickle.dump(df, protocol=5, buffer_callback=buffers.append, file=f)


for b in buffers:
    print(len(b.raw()))
# 24
# 24

# only prints 2 buffers! Expected 3 buffers (1 for each column)

随后我从另一个控制台运行consumer.py

import pandas as pd 
import numpy as np 
import pickle


buffers = [pickle.PickleBuffer(bytes(24)), pickle.PickleBuffer(bytes(24))]

f = open("my_df.pickle", "rb")
df = pickle.load(f, buffers=buffers)


print(df)
#   text  ints  floats
# 0    a     0     0.0
# 1    b     0     0.0
# 2    c     0     0.0

# Unexpected output. Numerical values are zero'd. And only 1 out of 3 columns ('text') is correctly populated. 

似乎 2 个 PickleBuffer 仅适用于数字列,但它们没有正确显示,而文本列却正确!

(显然,目的是正确地显示完整的 DataFrame。)

非常欢迎任何建议!

I'm attempting to achieve zero-copy sharing of a Pandas DataFrame between processes launched from seperate console sessions. Please consider the following two Python files:

producer.py:

import pandas as pd 
import numpy as np 
import pickle

df = pd.DataFrame({'text': ['a','b','c'], 'ints':[1,2,3], 'floats': [1.0,2,3]})


print(df)
#   text  ints  floats
# 0    a     1     1.0
# 1    b     2     2.0
# 2    c     3     3.0

# prints as expected!


buffers = []

with open("my_df.pickle", "wb") as f:
    pickle.dump(df, protocol=5, buffer_callback=buffers.append, file=f)


for b in buffers:
    print(len(b.raw()))
# 24
# 24

# only prints 2 buffers! Expected 3 buffers (1 for each column)

subsequently I run from another console consumer.py:

import pandas as pd 
import numpy as np 
import pickle


buffers = [pickle.PickleBuffer(bytes(24)), pickle.PickleBuffer(bytes(24))]

f = open("my_df.pickle", "rb")
df = pickle.load(f, buffers=buffers)


print(df)
#   text  ints  floats
# 0    a     0     0.0
# 1    b     0     0.0
# 2    c     0     0.0

# Unexpected output. Numerical values are zero'd. And only 1 out of 3 columns ('text') is correctly populated. 

It seems that the 2 PickleBuffers are for the numerical columns only, yet they are not brought across correctly, whilst the text column is!

(Obviously the intention is to bring across the full DataFrame correctly.)

Any advice most welcome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文