如何使用pyarrow将Pandas数据帧设置/获取到Redis中 [英] How to set/get Pandas dataframes into Redis using pyarrow

查看:650
本文介绍了如何使用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"))

现在,已弃用警告.

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和从Redis带回来.

How does pyarrow work? And, how do I get pyarrow objects into and back from Redis.

参考: 如何在Redis上设置/获取pandas.DataFrame?

推荐答案

下面是一个完整的示例,使用pyarrow对熊猫数据框进行序列化以存储在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

我刚刚将 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中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆