我正在尝试向 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'
知道如何安全地添加属性吗?
最佳答案
这与子类化无关。 Pandas 对象的属性不会序列化。
您可以阅读 this thread进行讨论和解决方法。该话题在 this other recent thread 中再次浮出水面.
关于python - pandas.DataFrame 子类的属性在 pickle 后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13250499/