Skip to content

ENH: Added optional pickle protocol version argument to pandas.to_pickle() #14526

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,7 @@ def to_sql(self, name, con, flavor=None, schema=None, if_exists='fail',
if_exists=if_exists, index=index, index_label=index_label,
chunksize=chunksize, dtype=dtype)

def to_pickle(self, path):
def to_pickle(self, path, protocol):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protocol should be an optional argument here. You'll probably need to have the default be None, and then determine the correct protocol based on the python version (see pandas.compat). The default should be the same as python's default.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually HIGHEST_PROTOCOL should work I think.

"""
Pickle (serialize) object to input file path.

Expand All @@ -1210,7 +1210,7 @@ def to_pickle(self, path):
File path
"""
from pandas.io.pickle import to_pickle
return to_pickle(self, path)
return to_pickle(self, path, protocol)

def to_clipboard(self, excel=None, sep=None, **kwargs):
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pandas.types.common import is_datetime64_dtype, _NS_DTYPE


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

Expand All @@ -17,7 +17,7 @@ def to_pickle(obj, path):
File path
"""
with open(path, 'wb') as f:
pkl.dump(obj, f, protocol=pkl.HIGHEST_PROTOCOL)
pkl.dump(obj, f, protocol=protocol)


def read_pickle(path):
Expand Down