如何使用pyarrow将Pandas数据帧设置/获取到Redis
使用
dd = {'ID': ['H576','H577','H578','H600', 'H700'], 'CD': ['AAAAAAA', 'BBBBB', 'CCCCCC','DDDDDD', 'EEEEEEE']}
df = pd.DataFrame(dd)
在Pandas 0.25之前,此方法适用。
set: redisConn.set("key", df.to_msgpack(compress='zlib'))get: pd.read_msgpack(redisConn.get("key"))
现在,已弃用警告。
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对象进出Redis。
回答:
这是一个使用pyarrow序列化熊猫数据帧以存储在Redis中的完整示例
apt-get install python3 python3-pip redis-serverpip3 install pandas pyarrow redis
然后在python中
import pandas as pdimport 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
我刚刚向熊猫提交了PR 28494,以便在文档中包含这个pyarrow示例。
以上是 如何使用pyarrow将Pandas数据帧设置/获取到Redis 的全部内容, 来源链接: utcz.com/qa/430712.html