无法解开从pandas DataFrame继承的类 [英] can't unpickle class that inherits from pandas DataFrame

查看:116
本文介绍了无法解开从pandas DataFrame继承的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试腌制从pandas.DataFrame继承的对象. 我添加到数据框的属性在酸洗/取消酸洗过程中消失了.有一些明显的解决方法,但是...我是在做错什么,还是这是个错误?

I'm trying to pickle objects that inherit from pandas.DataFrame. The attribute I add to the dataframe disappears during the pickling/unpickling process. There are some obvious workarounds, but... am I doing something wrong, or is this a bug?

import pandas as pd
import pickle

class Foo(pd.DataFrame):
    def __init__(self,tag,df):
        super().__init__(df)
        self._tag = tag

foo = Foo('mytag', pd.DataFrame({'a':[1,2,3],'b':[4,5,6]}))
print(foo)
print(foo._tag)

print("-------------------------------------")

with open("foo.pkl", "wb") as pkl:
    pickle.dump(foo, pkl)

with open("foo.pkl", "rb") as pkl:
    foo1 = pickle.load(pkl)

print(type(foo1))
print(foo1)
print(foo1._tag)

这是我的输出:

   a  b
0  1  4
1  2  5
2  3  6
mytag
-------------------------------------
<class '__main__.Foo'>
   a  b
0  1  4
1  2  5
2  3  6
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-20-1e7e89e199c8> in <module>
     21 print(type(foo1))
     22 print(foo1)
---> 23 print(foo1._tag)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   5065             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   5066                 return self[name]
-> 5067             return object.__getattribute__(self, name)
   5068 
   5069     def __setattr__(self, name, value):

AttributeError: 'Foo' object no attribute '_tag'

(python 3.7,pandas 0.24.2,pickle.format_version 4.0)

(python 3.7, pandas 0.24.2, pickle.format_version 4.0)

推荐答案

我真奇怪,我发布了

How strange, I posted a similar question at almost the same time. And in a follow-up remark, I've discovered something even more basic: meta-data you define yourself in a DataFrame subclass does not even survive SLICING operations.

创建foo实例后,将其打印并打印foo._tag,请尝试以下操作:

After you create your instance of foo, print it, and print foo._tag, try this:

bar = foo[1:]
print(bar)
print(bar._tag)

这还会返回一个AttributeError,与您的酱菜解开操作相同.

This also returns an AttributeError, same as your pickle-unpickle operation.

切片时可能有充分的理由来更改甚至删除元数据.但是您可能很想保留它.我不知道Pandas代码中是否有一个点会同时影响切片和酸洗,但我怀疑确实存在.

There might be good reasons to change or even remove meta-data when you slice. But you might very well want to preserve it. I don't know whether there is a single point in the Pandas code which affects both slicing and pickling, but I suspect there is.

这篇关于无法解开从pandas DataFrame继承的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆