简体   繁体   中英

how to pickle pandas DataFrame with name

I want to pickle pandas DataFrame with name. But it seems to fail as I show below.

Please tell me how to pickle DataFrame with name.

pandas version: 0.23.4

Thnaks.

input

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)

output

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'

In pickle framework, the save and load api is the following implementation:

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

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

The __dict__ in class DataFrame doesn't have the attribute name , so it will not be updated.

If you want to pickle your custom attribute, you can implement your custom DataFrame class with name . It looks like:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM