Created
July 23, 2014 16:48
-
-
Save lebedov/3bc70aa9fd73de940f26 to your computer and use it in GitHub Desktop.
Serialize/unserialize a class with a pandas data structure attribute using msgpack.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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