Skip to content

Commit 12de69b

Browse files
mingglijorisvandenbossche
authored andcommitted
DOC: update the to_pickle & read_pickle docstring (#20253)
1 parent 3783ccc commit 12de69b

File tree

2 files changed

+113
-18
lines changed

2 files changed

+113
-18
lines changed

pandas/core/generic.py

+38-7
Original file line numberDiff line numberDiff line change
@@ -2039,28 +2039,59 @@ def to_sql(self, name, con, schema=None, if_exists='fail', index=True,
20392039
def to_pickle(self, path, compression='infer',
20402040
protocol=pkl.HIGHEST_PROTOCOL):
20412041
"""
2042-
Pickle (serialize) object to input file path.
2042+
Pickle (serialize) object to file.
20432043
20442044
Parameters
20452045
----------
2046-
path : string
2047-
File path
2046+
path : str
2047+
File path where the pickled object will be stored.
20482048
compression : {'infer', 'gzip', 'bz2', 'xz', None}, default 'infer'
2049-
a string representing the compression to use in the output file
2049+
A string representing the compression to use in the output file. By
2050+
default, infers from the file extension in specified path.
20502051
20512052
.. versionadded:: 0.20.0
20522053
protocol : int
20532054
Int which indicates which protocol should be used by the pickler,
20542055
default HIGHEST_PROTOCOL (see [1], paragraph 12.1.2). The possible
20552056
values for this parameter depend on the version of Python. For
20562057
Python 2.x, possible values are 0, 1, 2. For Python>=3.0, 3 is a
2057-
valid value. For Python >= 3.4, 4 is a valid value.A negative value
2058-
for the protocol parameter is equivalent to setting its value to
2059-
HIGHEST_PROTOCOL.
2058+
valid value. For Python >= 3.4, 4 is a valid value. A negative
2059+
value for the protocol parameter is equivalent to setting its value
2060+
to HIGHEST_PROTOCOL.
20602061
20612062
.. [1] https://docs.python.org/3/library/pickle.html
20622063
.. versionadded:: 0.21.0
20632064
2065+
See Also
2066+
--------
2067+
read_pickle : Load pickled pandas object (or any object) from file.
2068+
DataFrame.to_hdf : Write DataFrame to an HDF5 file.
2069+
DataFrame.to_sql : Write DataFrame to a SQL database.
2070+
DataFrame.to_parquet : Write a DataFrame to the binary parquet format.
2071+
2072+
Examples
2073+
--------
2074+
>>> original_df = pd.DataFrame({"foo": range(5), "bar": range(5, 10)})
2075+
>>> original_df
2076+
foo bar
2077+
0 0 5
2078+
1 1 6
2079+
2 2 7
2080+
3 3 8
2081+
4 4 9
2082+
>>> original_df.to_pickle("./dummy.pkl")
2083+
2084+
>>> unpickled_df = pd.read_pickle("./dummy.pkl")
2085+
>>> unpickled_df
2086+
foo bar
2087+
0 0 5
2088+
1 1 6
2089+
2 2 7
2090+
3 3 8
2091+
4 4 9
2092+
2093+
>>> import os
2094+
>>> os.remove("./dummy.pkl")
20642095
"""
20652096
from pandas.io.pickle import to_pickle
20662097
return to_pickle(self, path, compression=compression,

pandas/io/pickle.py

+75-11
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@
1010

1111
def to_pickle(obj, path, compression='infer', protocol=pkl.HIGHEST_PROTOCOL):
1212
"""
13-
Pickle (serialize) object to input file path
13+
Pickle (serialize) object to file.
1414
1515
Parameters
1616
----------
1717
obj : any object
18-
path : string
19-
File path
18+
Any python object.
19+
path : str
20+
File path where the pickled object will be stored.
2021
compression : {'infer', 'gzip', 'bz2', 'xz', None}, default 'infer'
21-
a string representing the compression to use in the output file
22+
A string representing the compression to use in the output file. By
23+
default, infers from the file extension in specified path.
2224
2325
.. versionadded:: 0.20.0
2426
protocol : int
@@ -33,7 +35,36 @@ def to_pickle(obj, path, compression='infer', protocol=pkl.HIGHEST_PROTOCOL):
3335
.. [1] https://docs.python.org/3/library/pickle.html
3436
.. versionadded:: 0.21.0
3537
36-
38+
See Also
39+
--------
40+
read_pickle : Load pickled pandas object (or any object) from file.
41+
DataFrame.to_hdf : Write DataFrame to an HDF5 file.
42+
DataFrame.to_sql : Write DataFrame to a SQL database.
43+
DataFrame.to_parquet : Write a DataFrame to the binary parquet format.
44+
45+
Examples
46+
--------
47+
>>> original_df = pd.DataFrame({"foo": range(5), "bar": range(5, 10)})
48+
>>> original_df
49+
foo bar
50+
0 0 5
51+
1 1 6
52+
2 2 7
53+
3 3 8
54+
4 4 9
55+
>>> pd.to_pickle(original_df, "./dummy.pkl")
56+
57+
>>> unpickled_df = pd.read_pickle("./dummy.pkl")
58+
>>> unpickled_df
59+
foo bar
60+
0 0 5
61+
1 1 6
62+
2 2 7
63+
3 3 8
64+
4 4 9
65+
66+
>>> import os
67+
>>> os.remove("./dummy.pkl")
3768
"""
3869
path = _stringify_path(path)
3970
inferred_compression = _infer_compression(path, compression)
@@ -51,16 +82,17 @@ def to_pickle(obj, path, compression='infer', protocol=pkl.HIGHEST_PROTOCOL):
5182

5283
def read_pickle(path, compression='infer'):
5384
"""
54-
Load pickled pandas object (or any other pickled object) from the specified
55-
file path
85+
Load pickled pandas object (or any object) from file.
86+
87+
.. warning::
5688
57-
Warning: Loading pickled data received from untrusted sources can be
58-
unsafe. See: https://docs.python.org/3/library/pickle.html
89+
Loading pickled data received from untrusted sources can be
90+
unsafe. See `here <https://docs.python.org/3/library/pickle.html>`__.
5991
6092
Parameters
6193
----------
62-
path : string
63-
File path
94+
path : str
95+
File path where the pickled object will be loaded.
6496
compression : {'infer', 'gzip', 'bz2', 'xz', 'zip', None}, default 'infer'
6597
For on-the-fly decompression of on-disk data. If 'infer', then use
6698
gzip, bz2, xz or zip if path ends in '.gz', '.bz2', '.xz',
@@ -72,6 +104,38 @@ def read_pickle(path, compression='infer'):
72104
Returns
73105
-------
74106
unpickled : type of object stored in file
107+
108+
See Also
109+
--------
110+
DataFrame.to_pickle : Pickle (serialize) DataFrame object to file.
111+
Series.to_pickle : Pickle (serialize) Series object to file.
112+
read_hdf : Read HDF5 file into a DataFrame.
113+
read_sql : Read SQL query or database table into a DataFrame.
114+
read_parquet : Load a parquet object, returning a DataFrame.
115+
116+
Examples
117+
--------
118+
>>> original_df = pd.DataFrame({"foo": range(5), "bar": range(5, 10)})
119+
>>> original_df
120+
foo bar
121+
0 0 5
122+
1 1 6
123+
2 2 7
124+
3 3 8
125+
4 4 9
126+
>>> pd.to_pickle(original_df, "./dummy.pkl")
127+
128+
>>> unpickled_df = pd.read_pickle("./dummy.pkl")
129+
>>> unpickled_df
130+
foo bar
131+
0 0 5
132+
1 1 6
133+
2 2 7
134+
3 3 8
135+
4 4 9
136+
137+
>>> import os
138+
>>> os.remove("./dummy.pkl")
75139
"""
76140
path = _stringify_path(path)
77141
inferred_compression = _infer_compression(path, compression)

0 commit comments

Comments
 (0)