使用从Pandas DataFrame派生的类的pickle丢失了对象属性

时间:2018-05-10 13:24:54

标签: python pandas class

我正在尝试更新派生类的酸洗行为,但我无法将对象属性保存在酸洗中。

import pandas as pd

class Fruit(pd.DataFrame): 
    def __init__(self):
        super(Fruit, self).__init__()
        self.codebook={3:4}

sdf = Fruit()

sdf.codebook ={1:2}

import pickle

with open('mypickle.pickle', 'wb') as f:
    pickle.dump(sdf, f)
with open('mypickle.pickle') as f:
    loaded_obj = pickle.load(f)        
assert  loaded_obj.__class__ == Fruit
assert loaded_obj.codebook

最终断言失败。 AttributeError: 'Fruit' object has no attribute 'codebook'

如果我的班级定义是class Fruit(): pass,那么它可以正常工作。

如何挑选我的对象并添加新的“点”属性(.codebook)?

(此问题与向Pandas DataFrame类添加调查数据代码簿信息和功能的开放项目有关:https://github.com/cpbl/surveypandas

更新:

我尝试用Fruit自己替换DataFrame.to_pickle,它直接使用pickle.dump(self),并且以同样的方式失败。 (为什么?)

这是一个不成功的努力,它成功记录了所有内容,但却导致了pandas.read_pickle()无法理解的内容。这似乎也不必要地尴尬:

from pandas import DataFrame 
import cPickle as pkl

class Fruit(DataFrame): 
    def __init__(self, data=None, codebook =None):
        super(Fruit, self).__init__(data)
        self.codebook=codebook
    def to_pickle(self, path, compression='infer',
              protocol= pkl.HIGHEST_PROTOCOL):
        """ See pandas.io.pickle and pandas.DataFrame.to_pickle """
        from pandas.io.pickle import to_pickle as pandas_to_pickle
        return pandas_to_pickle( {'o': self, 'c':self.codebook}, path, compression=compression, protocol=protocol)

sdf = Fruit({'foo':[1,2,3]}, codebook={1:2})
sdf.to_pickle('mypickle.pickle')

with open('mypickle.pickle') as f:
    loaded_obj = pkl.load(f)
sdf2 = Fruit(loaded_obj['o'], codebook = loaded_obj['c'])

assert sdf2.__class__ == Fruit
assert sdf2.codebook

0 个答案:

没有答案