腌制后pandas.DataFrame的子类的属性消失 [英] Attributes to a subclass of pandas.DataFrame disappear after pickle
本文介绍了腌制后pandas.DataFrame的子类的属性消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将属性添加到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'
有什么想法可以安全地添加属性吗?
解决方案
这与子类化无关.熊猫对象的属性不会序列化.
您可以阅读此主题进行讨论和解决方法. 其他最新主题中再次出现了这个话题. /p>
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?
解决方案
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.
这篇关于腌制后pandas.DataFrame的子类的属性消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文