Skip to content

Instantly share code, notes, and snippets.

@lebedov
Created July 23, 2014 16:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lebedov/3bc70aa9fd73de940f26 to your computer and use it in GitHub Desktop.
Save lebedov/3bc70aa9fd73de940f26 to your computer and use it in GitHub Desktop.
Serialize/unserialize a class with a pandas data structure attribute using msgpack.
#!/usr/bin/env python
"""
Serialize/unserialize a class with a pandas data structure attribute using msgpack.
"""
import msgpack
import numpy as np
import pandas as pd
import pandas.io.packers
class Foo(object):
def __init__(self, data=None):
self.df = pandas.DataFrame(data)
def encode(obj):
if isinstance(obj, Foo):
print 'Foo'
return {'typ': 'Foo',
'data': obj.df}
else:
print 'Pandas'
return pandas.io.packers.encode(obj)
def decode(obj):
typ = obj.get('typ')
if typ == 'Foo':
f = Foo()
f.df = obj['data']
return f
else:
return pandas.io.packers.decode(obj)
f = Foo(pd.DataFrame(np.random.rand(100)))
# Serialization/deserialization doesn't work without setting
# the encoding:
s = msgpack.packb(f, default=encode, encoding='latin1')
f_new = msgpack.unpackb(s, object_hook=decode, encoding='latin1')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment