Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attributes to a subclass of pandas.DataFrame disappear after pickle

I am trying to add attributes to a subclass of pandas.DataFrame and they disappear after pickling and unpickling:

import cPickle
import pandas as pd

class MyClass(pd.DataFrame):
    def __init__(self):
        super(MyClass, self).__init__()
        self.bar = 1

myc = MyClass()
with open('myc.pickle', 'wb')as myfile:
    cPickle.dump(myc,myfile)
with open('myc.pickle', 'rb')as myfile:
    b = cPickle.load(myfile)
print b.bar

Output:

Traceback (most recent call last):
File "test_df.py", line 14, in <module>
print b.bar
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1771, in __getattr__
(type(self).__name__, name))
AttributeError: 'MyClass' object has no attribute 'bar'

Any idea how I can add attributes safely?

like image 924
Yariv Avatar asked Nov 06 '12 11:11

Yariv


People also ask

Can a Pandas DataFrame be pickled?

Pandas DataFrame: to_pickle() functionThe to_pickle() function is used to pickle (serialize) object to file. File path where the pickled object will be stored. A string representing the compression to use in the output file. By default, infers from the file extension in specified path.

Does Pandas maintain order?

Pandas. DataFrame doesn't preserve the column order when converting from a DataFrames.

Are Pandas series immutable?

All pandas data structures are value-mutable (the values they contain can be altered) but not always size-mutable. The length of a Series cannot be changed, but, for example, columns can be inserted into a DataFrame.

Can Pandas DataFrame hold different types?

A DataFrame is a 2-dimensional data structure that can store data of different types (including characters, integers, floating point values, categorical data and more) in columns.


1 Answers

This is unrelated to subclassing. Pandas objects' attributes do not serialize.

You can read this thread for a discussion and a workaround. The topic has resurfaced again in this other recent thread.

like image 132
meteore Avatar answered Sep 23 '22 22:09

meteore