如何用名称腌熊猫DataFrame

时间:2019-03-11 05:33:56

标签: python pandas

我想用名称来腌制熊猫DataFrame。 但这似乎失败了,如下所示。

请告诉我如何用名称腌制DataFrame。

熊猫版本:0.23.4

感谢。

输入

df = pd.DataFrame({'a':[0,1]})
df.name = 'test'
print(df.name)
with open('../data/intermediate/test.pickle', 'wb') as f:
    pickle.dump(df, f)

with open('../data/intermediate/test.pickle', 'rb') as f:
    test = pickle.load(f)
print(test.name)

输出

test
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-22a78fcd97e8> in <module>()
      7 with open('../data/intermediate/test.pickle', 'rb') as f:
      8     test = pickle.load(f)
----> 9 print(test.name)

~\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   4374             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   4375                 return self[name]
-> 4376             return object.__getattribute__(self, name)
   4377 
   4378     def __setattr__(self, name, value):

AttributeError: 'DataFrame' object has no attribute 'name'

1 个答案:

答案 0 :(得分:2)

pickle框架中,saveload api是以下实现:

def save(obj):
    return (obj.__class__, obj.__dict__)

def load(cls, attributes):
    obj = cls.__new__(cls)
    obj.__dict__.update(attributes)
    return obj

__dict__中的DataFrame没有属性name,因此不会被更新。

如果您要腌制您的自定义属性,则可以使用DataFrame来实现自定义name类。看起来像:

class AdvancedDataFrame(pd.DataFrame):

    def __init__(self, *args, **kwargs):
        self.name = kwargs.pop('name') if 'name' in kwargs else None
        super(AdvancedDataFrame, self).__init__(*args, **kwargs)