1
1
""" pickle compat """
2
2
import pickle
3
+ from typing import Any , Optional
3
4
import warnings
4
5
6
+ from pandas ._typing import FilePathOrBuffer
5
7
from pandas .compat import pickle_compat as pc
6
8
7
- from pandas .io .common import get_handle , stringify_path
9
+ from pandas .io .common import get_filepath_or_buffer , get_handle
8
10
9
11
10
- def to_pickle (obj , path , compression = "infer" , protocol = pickle .HIGHEST_PROTOCOL ):
12
+ def to_pickle (
13
+ obj : Any ,
14
+ filepath_or_buffer : FilePathOrBuffer ,
15
+ compression : Optional [str ] = "infer" ,
16
+ protocol : int = pickle .HIGHEST_PROTOCOL ,
17
+ ):
11
18
"""
12
19
Pickle (serialize) object to file.
13
20
14
21
Parameters
15
22
----------
16
23
obj : any object
17
24
Any python object.
18
- path : str
19
- File path where the pickled object will be stored.
25
+ filepath_or_buffer : str, path object or file-like object
26
+ File path, URL, or buffer where the pickled object will be stored.
27
+
28
+ .. versionchanged:: 1.0.0
29
+ Accept URL. URL has to be of S3 or GCS.
30
+
20
31
compression : {'infer', 'gzip', 'bz2', 'zip', 'xz', None}, default 'infer'
21
- A string representing the compression to use in the output file. By
22
- default, infers from the file extension in specified path.
32
+ If 'infer' and 'path_or_url' is path-like, then detect compression from
33
+ the following extensions: '.gz', '.bz2', '.zip', or '.xz' (otherwise no
34
+ compression) If 'infer' and 'path_or_url' is not path-like, then use
35
+ None (= no decompression).
23
36
protocol : int
24
37
Int which indicates which protocol should be used by the pickler,
25
38
default HIGHEST_PROTOCOL (see [1], paragraph 12.1.2). The possible
@@ -63,8 +76,12 @@ def to_pickle(obj, path, compression="infer", protocol=pickle.HIGHEST_PROTOCOL):
63
76
>>> import os
64
77
>>> os.remove("./dummy.pkl")
65
78
"""
66
- path = stringify_path (path )
67
- f , fh = get_handle (path , "wb" , compression = compression , is_text = False )
79
+ fp_or_buf , _ , compression , should_close = get_filepath_or_buffer (
80
+ filepath_or_buffer , compression = compression , mode = "wb"
81
+ )
82
+ if not isinstance (fp_or_buf , str ) and compression == "infer" :
83
+ compression = None
84
+ f , fh = get_handle (fp_or_buf , "wb" , compression = compression , is_text = False )
68
85
if protocol < 0 :
69
86
protocol = pickle .HIGHEST_PROTOCOL
70
87
try :
@@ -73,9 +90,16 @@ def to_pickle(obj, path, compression="infer", protocol=pickle.HIGHEST_PROTOCOL):
73
90
f .close ()
74
91
for _f in fh :
75
92
_f .close ()
93
+ if should_close :
94
+ try :
95
+ fp_or_buf .close ()
96
+ except ValueError :
97
+ pass
76
98
77
99
78
- def read_pickle (path , compression = "infer" ):
100
+ def read_pickle (
101
+ filepath_or_buffer : FilePathOrBuffer , compression : Optional [str ] = "infer"
102
+ ):
79
103
"""
80
104
Load pickled pandas object (or any object) from file.
81
105
@@ -86,13 +110,17 @@ def read_pickle(path, compression="infer"):
86
110
87
111
Parameters
88
112
----------
89
- path : str
90
- File path where the pickled object will be loaded.
113
+ filepath_or_buffer : str, path object or file-like object
114
+ File path, URL, or buffer where the pickled object will be loaded from.
115
+
116
+ .. versionchanged:: 1.0.0
117
+ Accept URL. URL is not limited to S3 and GCS.
118
+
91
119
compression : {'infer', 'gzip', 'bz2', 'zip', 'xz', None}, default 'infer'
92
- For on-the-fly decompression of on-disk data. If 'infer', then use
93
- gzip, bz2, xz or zip if path ends in '.gz', '.bz2', '.xz',
94
- or '.zip' respectively, and no decompression otherwise.
95
- Set to None for no decompression.
120
+ If 'infer' and 'path_or_url' is path-like , then detect compression from
121
+ the following extensions: '.gz', '.bz2', '.zip', or '.xz' (otherwise no
122
+ compression) If 'infer' and 'path_or_url' is not path-like, then use
123
+ None (= no decompression) .
96
124
97
125
Returns
98
126
-------
@@ -134,8 +162,12 @@ def read_pickle(path, compression="infer"):
134
162
>>> import os
135
163
>>> os.remove("./dummy.pkl")
136
164
"""
137
- path = stringify_path (path )
138
- f , fh = get_handle (path , "rb" , compression = compression , is_text = False )
165
+ fp_or_buf , _ , compression , should_close = get_filepath_or_buffer (
166
+ filepath_or_buffer , compression = compression
167
+ )
168
+ if not isinstance (fp_or_buf , str ) and compression == "infer" :
169
+ compression = None
170
+ f , fh = get_handle (fp_or_buf , "rb" , compression = compression , is_text = False )
139
171
140
172
# 1) try standard library Pickle
141
173
# 2) try pickle_compat (older pandas version) to handle subclass changes
@@ -159,3 +191,8 @@ def read_pickle(path, compression="infer"):
159
191
f .close ()
160
192
for _f in fh :
161
193
_f .close ()
194
+ if should_close :
195
+ try :
196
+ fp_or_buf .close ()
197
+ except ValueError :
198
+ pass
0 commit comments