本文介绍了如何使用 pyarrow 将 Pandas 数据帧设置/获取到 Redis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用
dd = {'ID': ['H576','H577','H578','H600', 'H700'],
'CD': ['AAAAAAA', 'BBBBB', 'CCCCCC','DDDDDD', 'EEEEEEE']}
df = pd.DataFrame(dd)
Pandas 0.25 之前的版本,这在下面起作用了.
Pre Pandas 0.25, this below worked.
set: redisConn.set("key", df.to_msgpack(compress='zlib'))
get: pd.read_msgpack(redisConn.get("key"))
现在,有已弃用的警告..
Now, there are deprecated warnings..
FutureWarning: to_msgpack is deprecated and will be removed in a future version.
It is recommended to use pyarrow for on-the-wire transmission of pandas objects.
The read_msgpack is deprecated and will be removed in a future version.
It is recommended to use pyarrow for on-the-wire transmission of pandas objects.
pyarrow 是如何工作的?而且,我如何将 pyarrow 对象传入和传回 Redis.
How does pyarrow work? And, how do I get pyarrow objects into and back from Redis.
参考:如何设置/从Redis获取pandas.DataFrame?
推荐答案
这是一个使用 pyarrow 序列化 Pandas 数据帧以存储在 redis 中的完整示例
Here's a full example to use pyarrow for serialization of a pandas dataframe to store in redis
apt-get install python3 python3-pip redis-server
pip3 install pandas pyarrow redis
然后在python中
import pandas as pd
import pyarrow as pa
import redis
df=pd.DataFrame({'A':[1,2,3]})
r = redis.Redis(host='localhost', port=6379, db=0)
context = pa.default_serialization_context()
r.set("key", context.serialize(df).to_buffer().to_pybytes())
context.deserialize(r.get("key"))
A
0 1
1 2
2 3
我刚刚向 pandas 提交了 PR 28494 以将这个 pyarrow 示例包含在文档.
I just submitted PR 28494 to pandas to include this pyarrow example in the docs.
参考文档:
- https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_msgpack.html
- https://arrow.apache.org/docs/python/ipc.html#arbitrary-object-serialization
- https://arrow.apache.org/docs/python/memory.html#pyarrow-buffer
- https://stackoverflow.com/a/37957490/4126114
这篇关于如何使用 pyarrow 将 Pandas 数据帧设置/获取到 Redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!