Yar*_*riv 4 python inheritance class subclass pandas
我正在尝试将属性添加到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
Run Code Online (Sandbox Code Playgroud)
输出:
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'
Run Code Online (Sandbox Code Playgroud)
知道如何安全地添加属性吗?