R. Y
R. Y

Reputation: 49

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

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'

Upvotes: 0

Views: 594

Answers (1)

cloudyyyyy
cloudyyyyy

Reputation: 491

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)

Upvotes: 3

Related Questions