1010import warnings
1111
1212from pandas .compat import pickle_compat
13- from pandas .util ._decorators import (
14- doc ,
15- set_module ,
16- )
17-
18- from pandas .core .shared_docs import _shared_docs
13+ from pandas .util ._decorators import set_module
1914
2015from pandas .io .common import get_handle
2116
3530
3631
3732@set_module ("pandas" )
38- @doc (
39- storage_options = _shared_docs ["storage_options" ],
40- compression_options = _shared_docs ["compression_options" ] % "filepath_or_buffer" ,
41- )
4233def to_pickle (
4334 obj : Any ,
4435 filepath_or_buffer : FilePath | WriteBuffer [bytes ],
@@ -57,8 +48,21 @@ def to_pickle(
5748 String, path object (implementing ``os.PathLike[str]``), or file-like
5849 object implementing a binary ``write()`` function.
5950 Also accepts URL. URL has to be of S3 or GCS.
60- {compression_options}
61-
51+ compression : str or dict, default 'infer'
52+ For on-the-fly compression of the output data. If 'infer' and
53+ 'filepath_or_buffer' is path-like, then detect compression from the
54+ following extensions: '.gz', '.bz2', '.zip', '.xz', '.zst', '.tar',
55+ '.tar.gz', '.tar.xz' or '.tar.bz2' (otherwise no compression).
56+ Set to ``None`` for no compression.
57+ Can also be a dict with key ``'method'`` set
58+ to one of {``'zip'``, ``'gzip'``, ``'bz2'``, ``'zstd'``, ``'xz'``,
59+ ``'tar'``} and other key-value pairs are forwarded to
60+ ``zipfile.ZipFile``, ``gzip.GzipFile``,
61+ ``bz2.BZ2File``, ``zstandard.ZstdCompressor``, ``lzma.LZMAFile`` or
62+ ``tarfile.TarFile``, respectively.
63+ As an example, the following could be passed for faster compression
64+ and to create a reproducible gzip archive:
65+ ``compression={'method': 'gzip', 'compresslevel': 1, 'mtime': 1}``.
6266 protocol : int
6367 Int which indicates which protocol should be used by the pickler,
6468 default HIGHEST_PROTOCOL (see [1], paragraph 12.1.2). The possible
@@ -67,8 +71,15 @@ def to_pickle(
6771 For Python >= 3.4, 4 is a valid value. A negative value for the
6872 protocol parameter is equivalent to setting its value to
6973 HIGHEST_PROTOCOL.
70-
71- {storage_options}
74+ storage_options : dict, optional
75+ Extra options that make sense for a particular storage connection, e.g.
76+ host, port, username, password, etc. For HTTP(S) URLs the key-value pairs
77+ are forwarded to ``urllib.request.Request`` as header options. For other
78+ URLs (e.g. starting with "s3://", and "gcs://") the key-value pairs are
79+ forwarded to ``fsspec.open``. Please see ``fsspec`` and ``urllib`` for more
80+ details, and for more examples on storage options refer `here
81+ <https://pandas.pydata.org/docs/user_guide/io.html?
82+ highlight=storage_options#reading-writing-remote-files>`_.
7283
7384 .. [1] https://docs.python.org/3/library/pickle.html
7485
@@ -117,10 +128,6 @@ def to_pickle(
117128
118129
119130@set_module ("pandas" )
120- @doc (
121- storage_options = _shared_docs ["storage_options" ],
122- decompression_options = _shared_docs ["decompression_options" ] % "filepath_or_buffer" ,
123- )
124131def read_pickle (
125132 filepath_or_buffer : FilePath | ReadPickleBuffer ,
126133 compression : CompressionOptions = "infer" ,
@@ -140,10 +147,32 @@ def read_pickle(
140147 String, path object (implementing ``os.PathLike[str]``), or file-like
141148 object implementing a binary ``readlines()`` function.
142149 Also accepts URL. URL is not limited to S3 and GCS.
143-
144- {decompression_options}
145-
146- {storage_options}
150+ compression : str or dict, default 'infer'
151+ For on-the-fly decompression of on-disk data. If 'infer' and
152+ 'filepath_or_buffer' is path-like, then detect compression from the
153+ following extensions: '.gz', '.bz2', '.zip', '.xz', '.zst', '.tar',
154+ '.tar.gz', '.tar.xz' or '.tar.bz2' (otherwise no compression).
155+ If using 'zip' or 'tar', the ZIP file must contain only one data file
156+ to be read in.
157+ Set to ``None`` for no decompression.
158+ Can also be a dict with key ``'method'`` set
159+ to one of {``'zip'``, ``'gzip'``, ``'bz2'``, ``'zstd'``, ``'xz'``,
160+ ``'tar'``} and other key-value pairs are forwarded to
161+ ``zipfile.ZipFile``, ``gzip.GzipFile``,
162+ ``bz2.BZ2File``, ``zstandard.ZstdDecompressor``, ``lzma.LZMAFile`` or
163+ ``tarfile.TarFile``, respectively.
164+ As an example, the following could be passed for Zstandard decompression
165+ using a custom compression dictionary:
166+ ``compression={'method': 'zstd', 'dict_data': my_compression_dict}``.
167+ storage_options : dict, optional
168+ Extra options that make sense for a particular storage connection, e.g.
169+ host, port, username, password, etc. For HTTP(S) URLs the key-value pairs
170+ are forwarded to ``urllib.request.Request`` as header options. For other
171+ URLs (e.g. starting with "s3://", and "gcs://") the key-value pairs are
172+ forwarded to ``fsspec.open``. Please see ``fsspec`` and ``urllib`` for more
173+ details, and for more examples on storage options refer `here
174+ <https://pandas.pydata.org/docs/user_guide/io.html?
175+ highlight=storage_options#reading-writing-remote-files>`_.
147176
148177 Returns
149178 -------
0 commit comments