Skip to content

Subclassed attributes are not serializable #10553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kjordahl opened this issue Jul 11, 2015 · 2 comments
Closed

Subclassed attributes are not serializable #10553

kjordahl opened this issue Jul 11, 2015 · 2 comments
Labels
Bug Compat pandas objects compatability with Numpy or Python functions Subclassing Subclassing pandas objects
Milestone

Comments

@kjordahl
Copy link

In a subclass of pandas objects, pickling an object doesn't serialize properties of an instance of that subclass, even if the attribute has been added to _metadata. It would be hard to override this behavior entirely in the subclass, because it will require updates to __getstate__ and __setstate__, and probably also an addition or subclass of BlockManager. It would be nice if the _metadata serialization was handled in the base pandas class.

Example:

class SubDataFrame(DataFrame):

    _metadata = ['my_data']

    @property
    def _constructor(self):
        return SubDataFrame

sdf = SubDataFrame()
sdf.my_data = 'foo'
sdf.to_pickle('tmp.pkl')
new_sdf = read_pickle('tmp.pkl')
new_sdf.my_data

raises AttributeError: 'SubDataFrame' object has no attribute 'my_data'

(edited original example for correctness)

@jreback
Copy link
Contributor

jreback commented Jul 12, 2015

I think if this were replaces core/generic.py (and no need to have the Series.__getstate__ either)
then this would work (and be back-compat). The reconstruction accepts a dict of these things

 def __getstate__(self):
        meta = dict([ (k, getattr(self, k, None)) for k in self._metadata ])
        return dict(_data=self._data, **meta)

Want to do a pull-request (already lots of tests on prior version pickle recovery in pandas/io/tests/test_pickle.py so that will test the back-compat)

@jreback jreback added Bug Compat pandas objects compatability with Numpy or Python functions labels Jul 12, 2015
@jreback jreback added this to the 0.17.0 milestone Jul 12, 2015
@kjordahl
Copy link
Author

@jreback Thanks, that's simpler than I thought. I had to add a couple more attributes (_metadata and _typ) to make sure it gets deserialized. #10557 submitted.

@jorisvandenbossche jorisvandenbossche added the Subclassing Subclassing pandas objects label Dec 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Compat pandas objects compatability with Numpy or Python functions Subclassing Subclassing pandas objects
Projects
None yet
Development

No branches or pull requests

3 participants