How to set/get pandas.DataFrame to/from Redis?

后端 未结 4 865
孤城傲影
孤城傲影 2020-12-23 14:38

After setting a DataFrame to redis, then getting it back, redis returns a string and I can\'t figure out a way to convert this str to a DataFrame.

How can I do these

4条回答
  •  佛祖请我去吃肉
    2020-12-23 15:24

    For caching a dataframe use this.

    import pyarrow as pa
    
    def cache_df(alias,df):
    
        pool = redis.ConnectionPool(host='host', port='port', db='db')
        cur = redis.Redis(connection_pool=pool)
        context = pa.default_serialization_context()
        df_compressed =  context.serialize(df).to_buffer().to_pybytes()
    
        res = cur.set(alias,df_compressed)
        if res == True:
            print('df cached')
    

    For fetching the cached dataframe use this.

    def get_cached_df(alias):
    
        pool = redis.ConnectionPool(host='host',port='port', db='db') 
        cur = redis.Redis(connection_pool=pool)
        context = pa.default_serialization_context()
        all_keys = [key.decode("utf-8") for key in cur.keys()]
    
        if alias in all_keys:   
            result = cur.get(alias)
    
            dataframe = pd.DataFrame.from_dict(context.deserialize(result))
    
            return dataframe
    
        return None
    

提交回复
热议问题