Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 two appropriately?

like image 657
Alex Luya Avatar asked Jun 21 '16 11:06

Alex Luya


People also ask

Is PyArrow faster than pandas?

python - Pyarrow is slower than pandas for csv read in - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

Does pandas use PyArrow?

To interface with pandas, PyArrow provides various conversion routines to consume pandas structures and convert back to them.

Can PyArrow replace pandas?

Yes, pyarrow is a library for building data frame internals (and other data processing applications). It is not an end user library like pandas.

Is Panda faster than NP?

Pandas is more user-friendly, but NumPy is faster. Pandas has a lot more options for handling missing data, but NumPy has better performance on large datasets. Pandas uses Python objects internally, making it easier to work with than NumPy (which uses C arrays).


2 Answers

set:

redisConn.set("key", df.to_msgpack(compress='zlib')) 

get:

pd.read_msgpack(redisConn.get("key")) 
like image 176
Alex Luya Avatar answered Sep 20 '22 12:09

Alex Luya


I couldn't use msgpack because of Decimal objects in my dataframe. Instead I combined pickle and zlib together like this, assuming a dataframe df and a local instance of Redis:

import pickle import redis import zlib  EXPIRATION_SECONDS = 600  r = redis.StrictRedis(host='localhost', port=6379, db=0)  # Set r.setex("key", EXPIRATION_SECONDS, zlib.compress( pickle.dumps(df)))  # Get rehydrated_df = pickle.loads(zlib.decompress(r.get("key"))) 

There isn't anything dataframe specific about this.

Caveats

  • the other answer using msgpack is better -- use it if it works for you
  • pickling can be dangerous -- your Redis server needs to be secure or you're asking for trouble
like image 43
Mark Chackerian Avatar answered Sep 18 '22 12:09

Mark Chackerian