How To Set/get Pandas Dataframes Into Redis Using Pyarrow
Using dd = {'ID': ['H576','H577','H578','H600', 'H700'], 'CD': ['AAAAAAA', 'BBBBB', 'CCCCCC','DDDDDD', 'EEEEEEE']} df = pd.DataFrame(dd) Pre Pandas 0.25, this below worked.
Solution 1:
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
and then in 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
011223
I just submitted PR 28494 to pandas to include this pyarrow example in the docs.
Reference docs:
Solution 2:
Here is how I do it since default_serialization_context is deprecated and things are a bit simpler:
import pyarrow as pa
import redis
pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
r = redis.Redis(connection_pool=pool)
defstoreInRedis(alias, df):
df_compressed = pa.serialize(df).to_buffer().to_pybytes()
res = r.set(alias,df_compressed)
if res == True:
print(f'{alias} cached')
defloadFromRedis(alias):
data = r.get(alias)
try:
return pa.deserialize(data)
except:
print("No data")
storeInRedis('locations', locdf)
loadFromRedis('locations')
Solution 3:
If you would like to compress the data in Redis, you can use the built in support for parquet & gzip
defopenRedisCon():
pool = redis.ConnectionPool(host=REDIS_HOST, port=REDIS_PORT, db=0)
r = redis.Redis(connection_pool=pool)
return r
defstoreDFInRedis(alias, df):
"""Store the dataframe object in Redis
"""
buffer = io.BytesIO()
df.to_parquet(buffer, compression='gzip')
buffer.seek(0) # re-set the pointer to the beginning after reading
r = openRedisCon()
res = r.set(alias,buffer.read())
defloadDFFromRedis(alias, useStale: bool = False):
"""Load the named key from Redis into a DataFrame and return the DF object
"""
r = openRedisCon()
try:
buffer = io.BytesIO(r.get(alias))
buffer.seek(0)
df = pd.read_parquet(buffer)
return df
except:
returnNone
Post a Comment for "How To Set/get Pandas Dataframes Into Redis Using Pyarrow"