我正在尝试向 pandas 添加一种方法,以便在可以访问数据框的情况下轻松使用它。然而,序列化“杀死”了以下示例所示的方法
import dill
class Foo:
def sayhello(self):
print("hello")
f = Foo()
dill.dump(f, open("./foo.pickle", "wb"))
f1 = dill.load(open("./foo.pickle", "r"))
f1.sayhello()
def addto(instance):
def decorator(f):
import types
f = types.MethodType(f, instance, instance.__class__)
setattr(instance, f.func_name, f)
return f
return decorator
@addto(f)
def saygoodbye(self):
print("goodbye")
dill.dump(f, open("./foo.pickle", "wb"))
f1 = dill.load(open("./foo.pickle", "r"))
f1.sayhello()
f1.saygoodbye()
import pandas as pd
df = pd.DataFrame([0,1])
@addto(df)
def saygoodbye(self):
print("goodbye")
dill.dump(df, open("./dframe.pickle", "wb"))
df1 = dill.load(open("./dframe.pickle", "r"))
df1.saygoodbye()
这让我AttributeError: 'DataFrame' object has no attribute 'saygoodbye'
1)你看到是什么导致了问题吗?
2)您知道如何在数据帧上序列化添加的方法吗?
谢谢