-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Adding 'protocol' parameter to 'to_pickle'. #16252
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
Changes from 6 commits
66a35e8
04bc5c2
4bf0386
35f8d18
9c9d38f
352220b
460ca0c
7631146
14bc485
20a854d
8eb660d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,7 @@ | |
from pandas import compat | ||
from pandas.compat.numpy import function as nv | ||
from pandas.compat import (map, zip, lzip, lrange, string_types, | ||
isidentifier, set_function_name) | ||
isidentifier, set_function_name, cPickle as pkl) | ||
import pandas.core.nanops as nanops | ||
from pandas.util.decorators import Appender, Substitution, deprecate_kwarg | ||
from pandas.util.validators import validate_bool_kwarg | ||
|
@@ -1344,7 +1344,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. | ||
|
||
|
@@ -1356,9 +1357,21 @@ def to_pickle(self, path, compression='infer'): | |
a string representing the compression to use in the output file | ||
|
||
.. versionadded:: 0.20.0 | ||
protocol : int | ||
Int which indicates which protocol should be used by the pickler, | ||
default HIGHEST_PROTOCOL (Pickle module). The possible values for | ||
this parameter depend on the version of Python. For Python 2.x, | ||
possible values are 0, 1, 2. For Python>=3.0, 3 is a valid value. | ||
For Python >= 3.4, 4 is a valid value.A negative value for the | ||
protocol parameter is equivalent to setting its value to | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a link to the pickle docs where this is defined. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done, I added the url. |
||
HIGHEST_PROTOCOL. | ||
|
||
.. versionadded:: 0.21.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): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -20,13 +20,27 @@ def to_pickle(obj, path, compression='infer'): | |
a string representing the compression to use in the output file | ||
|
||
.. versionadded:: 0.20.0 | ||
protocol : int | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). The possible values for | ||
this parameter depend on the version of Python. For Python 2.x, | ||
possible values are 0, 1, 2. For Python>=3.0, 3 is a valid value. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
For Python >= 3.4, 4 is a valid value. A negative value for the | ||
protocol parameter is equivalent to setting its value to | ||
HIGHEST_PROTOCOL. | ||
|
||
.. versionadded:: 0.21.0 | ||
|
||
|
||
""" | ||
inferred_compression = _infer_compression(path, compression) | ||
f, fh = _get_handle(path, 'wb', | ||
compression=inferred_compression, | ||
is_text=False) | ||
if protocol < 0: | ||
protocol = pkl.HIGHEST_PROTOCOL | ||
try: | ||
pkl.dump(obj, f, protocol=pkl.HIGHEST_PROTOCOL) | ||
pkl.dump(obj, f, protocol=protocol) | ||
finally: | ||
for _f in fh: | ||
_f.close() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need this section. Can just add a single line with the issue refernce.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the 'whatsnew' file accordingly.