Skip to content

Adding optional pickle protocol version argument to pandas.to_pickle() #14488

Closed
@ragesz

Description

@ragesz

Sometimes pickle files saved in Python v3.x are needed to read in Python v2.x. It would be nice if one can easily set pickle protocol version in to_pickle().

It can be done with the following little changes:

In file /pandas/io/pickle.py:

def to_pickle(obj, path, protocol=pkl.HIGHEST_PROTOCOL):
    """
    Pickle (serialize) object to input file path

    Parameters
    ----------
    obj : any object
    path : string
        File path
    """
    with open(path, 'wb') as f:
        pkl.dump(obj, f, protocol=protocol)

In file /pandas/core/generic.py:

    def to_pickle(self, path, protocol):
        """
        Pickle (serialize) object to input file path.

        Parameters
        ----------
        path : string
            File path
        """
        from pandas.io.pickle import to_pickle
        return to_pickle(self, path, protocol)

Metadata

Metadata

Assignees

No one assigned

    Labels

    API DesignCompatpandas objects compatability with Numpy or Python functionsIO DataIO issues that don't fit into a more specific label

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions