Skip to content
Closed
10 changes: 8 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from pandas.compat.numpy import function as nv
from pandas.compat import (map, zip, lzip, lrange, string_types,
isidentifier, set_function_name)
from pandas.compat import cPickle as pkl
Copy link
Contributor

Choose a reason for hiding this comment

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

move to the previous line

import pandas.core.nanops as nanops
from pandas.util.decorators import Appender, Substitution, deprecate_kwarg
from pandas.util.validators import validate_bool_kwarg
Expand Down Expand Up @@ -1344,7 +1345,8 @@ 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, compression='infer'):
def to_pickle(self, path, compression='infer',
protocol=pkl.HIGHEST_PROTOCOL):
"""
Pickle (serialize) object to input file path.

Expand All @@ -1354,11 +1356,15 @@ def to_pickle(self, path, compression='infer'):
File path
compression : {'infer', 'gzip', 'bz2', 'xz', None}, default 'infer'
a string representing the compression to use in the output file
protocol : int
Int which indicates which protocol should be used by the pickler,
default HIGHEST_PROTOCOL (Pickle module).
Copy link
Contributor

Choose a reason for hiding this comment

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

version added tag (the compression one goes above this), your tag should read 0.21.0


.. versionadded:: 0.20.0
"""
from pandas.io.pickle import to_pickle
return to_pickle(self, path, compression=compression)
return to_pickle(self, path, compression=compression,
protocol=protocol)

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


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

Expand All @@ -18,6 +18,9 @@ def to_pickle(obj, path, compression='infer'):
File path
compression : {'infer', 'gzip', 'bz2', 'xz', None}, default 'infer'
a string representing the compression to use in the output file
protocol : int
Copy link
Contributor

Choose a reason for hiding this comment

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

same

Int which indicates which protocol should be used by the pickler,
default HIGHEST_PROTOCOL (Pickle module).

.. versionadded:: 0.20.0
"""
Expand All @@ -26,7 +29,7 @@ def to_pickle(obj, path, compression='infer'):
compression=inferred_compression,
is_text=False)
try:
pkl.dump(obj, f, protocol=pkl.HIGHEST_PROTOCOL)
pkl.dump(obj, f, protocol=protocol)
finally:
for _f in fh:
_f.close()
Expand Down