Attributes to a subclass of pandas.DataFrame disappear after pickle

[亡魂溺海] 提交于 2019-12-14 03:48:01

问题


I am trying to add attributes to a subclass of pandas.DataFrame and they disappear after pickling and unpickling:

import cPickle
import pandas as pd

class MyClass(pd.DataFrame):
    def __init__(self):
        super(MyClass, self).__init__()
        self.bar = 1

myc = MyClass()
with open('myc.pickle', 'wb')as myfile:
    cPickle.dump(myc,myfile)
with open('myc.pickle', 'rb')as myfile:
    b = cPickle.load(myfile)
print b.bar

Output:

Traceback (most recent call last):
File "test_df.py", line 14, in <module>
print b.bar
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1771, in __getattr__
(type(self).__name__, name))
AttributeError: 'MyClass' object has no attribute 'bar'

Any idea how I can add attributes safely?


回答1:


This is unrelated to subclassing. Pandas objects' attributes do not serialize.

You can read this thread for a discussion and a workaround. The topic has resurfaced again in this other recent thread.




回答2:


You can use the @property decorator to do something similar:

class MyClass(pd.DataFrame):
    def __init__(self, *args, **kwargs):
        super(MyClass, self).__init__(*args, **kwargs)
        self.foo = 1


    @property
    def bar(self):
        return 1

MyClass.foo will not be available after pickling, but MyClass.bar will be there (as of now, read only).



来源:https://stackoverflow.com/questions/13250499/attributes-to-a-subclass-of-pandas-dataframe-disappear-after-pickle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!