11""" pickle compat """
22import pickle
3+ from typing import Any , Optional
34import warnings
45
6+ from pandas ._typing import FilePathOrBuffer
57from pandas .compat import pickle_compat as pc
68
7- from pandas .io .common import get_handle , stringify_path
9+ from pandas .io .common import get_filepath_or_buffer , get_handle
810
911
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+ ):
1118 """
1219 Pickle (serialize) object to file.
1320
1421 Parameters
1522 ----------
1623 obj : any object
1724 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+
2031 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).
2336 protocol : int
2437 Int which indicates which protocol should be used by the pickler,
2538 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):
6376 >>> import os
6477 >>> os.remove("./dummy.pkl")
6578 """
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 )
6885 if protocol < 0 :
6986 protocol = pickle .HIGHEST_PROTOCOL
7087 try :
@@ -73,9 +90,16 @@ def to_pickle(obj, path, compression="infer", protocol=pickle.HIGHEST_PROTOCOL):
7390 f .close ()
7491 for _f in fh :
7592 _f .close ()
93+ if should_close :
94+ try :
95+ fp_or_buf .close ()
96+ except ValueError :
97+ pass
7698
7799
78- def read_pickle (path , compression = "infer" ):
100+ def read_pickle (
101+ filepath_or_buffer : FilePathOrBuffer , compression : Optional [str ] = "infer"
102+ ):
79103 """
80104 Load pickled pandas object (or any object) from file.
81105
@@ -86,13 +110,17 @@ def read_pickle(path, compression="infer"):
86110
87111 Parameters
88112 ----------
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+
91119 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) .
96124
97125 Returns
98126 -------
@@ -134,8 +162,12 @@ def read_pickle(path, compression="infer"):
134162 >>> import os
135163 >>> os.remove("./dummy.pkl")
136164 """
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 )
139171
140172 # 1) try standard library Pickle
141173 # 2) try pickle_compat (older pandas version) to handle subclass changes
@@ -159,3 +191,8 @@ def read_pickle(path, compression="infer"):
159191 f .close ()
160192 for _f in fh :
161193 _f .close ()
194+ if should_close :
195+ try :
196+ fp_or_buf .close ()
197+ except ValueError :
198+ pass
0 commit comments