Skip to content

Commit 66a35e8

Browse files
committed
Added 'protocol' parameter to 'to_pickle'.
1 parent 1f5ecc9 commit 66a35e8

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

pandas/core/generic.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
from pandas.compat.numpy import function as nv
5151
from pandas.compat import (map, zip, lzip, lrange, string_types,
5252
isidentifier, set_function_name)
53+
from pandas.compat import cPickle as pkl
5354
import pandas.core.nanops as nanops
5455
from pandas.util.decorators import Appender, Substitution, deprecate_kwarg
5556
from pandas.util.validators import validate_bool_kwarg
@@ -1344,7 +1345,8 @@ def to_sql(self, name, con, flavor=None, schema=None, if_exists='fail',
13441345
if_exists=if_exists, index=index, index_label=index_label,
13451346
chunksize=chunksize, dtype=dtype)
13461347

1347-
def to_pickle(self, path, compression='infer'):
1348+
def to_pickle(self, path, compression='infer',
1349+
protocol=pkl.HIGHEST_PROTOCOL):
13481350
"""
13491351
Pickle (serialize) object to input file path.
13501352
@@ -1354,11 +1356,15 @@ def to_pickle(self, path, compression='infer'):
13541356
File path
13551357
compression : {'infer', 'gzip', 'bz2', 'xz', None}, default 'infer'
13561358
a string representing the compression to use in the output file
1359+
protocol : int
1360+
Int which indicates which protocol should be used by the pickler,
1361+
default HIGHEST_PROTOCOL (Pickle module).
13571362
13581363
.. versionadded:: 0.20.0
13591364
"""
13601365
from pandas.io.pickle import to_pickle
1361-
return to_pickle(self, path, compression=compression)
1366+
return to_pickle(self, path, compression=compression,
1367+
protocol=protocol)
13621368

13631369
def to_clipboard(self, excel=None, sep=None, **kwargs):
13641370
"""

pandas/io/pickle.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pandas.io.common import _get_handle, _infer_compression
88

99

10-
def to_pickle(obj, path, compression='infer'):
10+
def to_pickle(obj, path, compression='infer', protocol=pkl.HIGHEST_PROTOCOL):
1111
"""
1212
Pickle (serialize) object to input file path
1313
@@ -18,6 +18,9 @@ def to_pickle(obj, path, compression='infer'):
1818
File path
1919
compression : {'infer', 'gzip', 'bz2', 'xz', None}, default 'infer'
2020
a string representing the compression to use in the output file
21+
protocol : int
22+
Int which indicates which protocol should be used by the pickler,
23+
default HIGHEST_PROTOCOL (Pickle module).
2124
2225
.. versionadded:: 0.20.0
2326
"""
@@ -26,7 +29,7 @@ def to_pickle(obj, path, compression='infer'):
2629
compression=inferred_compression,
2730
is_text=False)
2831
try:
29-
pkl.dump(obj, f, protocol=pkl.HIGHEST_PROTOCOL)
32+
pkl.dump(obj, f, protocol=protocol)
3033
finally:
3134
for _f in fh:
3235
_f.close()

0 commit comments

Comments
 (0)