10
10
11
11
def to_pickle (obj , path , compression = 'infer' , protocol = pkl .HIGHEST_PROTOCOL ):
12
12
"""
13
- Pickle (serialize) object to input file path
13
+ Pickle (serialize) object to file.
14
14
15
15
Parameters
16
16
----------
17
17
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.
20
21
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.
22
24
23
25
.. versionadded:: 0.20.0
24
26
protocol : int
@@ -33,7 +35,36 @@ def to_pickle(obj, path, compression='infer', protocol=pkl.HIGHEST_PROTOCOL):
33
35
.. [1] https://docs.python.org/3/library/pickle.html
34
36
.. versionadded:: 0.21.0
35
37
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")
37
68
"""
38
69
path = _stringify_path (path )
39
70
inferred_compression = _infer_compression (path , compression )
@@ -51,16 +82,17 @@ def to_pickle(obj, path, compression='infer', protocol=pkl.HIGHEST_PROTOCOL):
51
82
52
83
def read_pickle (path , compression = 'infer' ):
53
84
"""
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::
56
88
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>`__.
59
91
60
92
Parameters
61
93
----------
62
- path : string
63
- File path
94
+ path : str
95
+ File path where the pickled object will be loaded.
64
96
compression : {'infer', 'gzip', 'bz2', 'xz', 'zip', None}, default 'infer'
65
97
For on-the-fly decompression of on-disk data. If 'infer', then use
66
98
gzip, bz2, xz or zip if path ends in '.gz', '.bz2', '.xz',
@@ -72,6 +104,38 @@ def read_pickle(path, compression='infer'):
72
104
Returns
73
105
-------
74
106
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")
75
139
"""
76
140
path = _stringify_path (path )
77
141
inferred_compression = _infer_compression (path , compression )
0 commit comments