我正在尝试将属性添加到 pandas.DataFrame 的子类,它们在酸洗和解酸后消失:
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
输出:
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'
知道如何安全地添加属性吗?