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,24 @@ 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 'filepath_or_buffer' is
53+ path-like, then detect compression from the following extensions: '.gz',
54+ '.bz2', '.zip', '.xz', '.zst', '.tar', '.tar.gz', '.tar.xz' or '.tar.bz2'
55+ (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'``, ``'tar'``} and
59+ 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 and to create
64+ a reproducible gzip archive:
65+ ``compression={'method': 'gzip', 'compresslevel': 1, 'mtime': 1}``.
66+
67+ .. versionadded:: 1.5.0
68+ Added support for `.tar` files.
6269 protocol : int
6370 Int which indicates which protocol should be used by the pickler,
6471 default HIGHEST_PROTOCOL (see [1], paragraph 12.1.2). The possible
@@ -67,8 +74,15 @@ def to_pickle(
6774 For Python >= 3.4, 4 is a valid value. A negative value for the
6875 protocol parameter is equivalent to setting its value to
6976 HIGHEST_PROTOCOL.
70-
71- {storage_options}
77+ storage_options : dict, optional
78+ Extra options that make sense for a particular storage connection, e.g.
79+ host, port, username, password, etc. For HTTP(S) URLs the key-value pairs
80+ are forwarded to ``urllib.request.Request`` as header options. For other
81+ URLs (e.g. starting with "s3://", and "gcs://") the key-value pairs are
82+ forwarded to ``fsspec.open``. Please see ``fsspec`` and ``urllib`` for more
83+ details, and for more examples on storage options refer `here
84+ <https://pandas.pydata.org/docs/user_guide/io.html?
85+ highlight=storage_options#reading-writing-remote-files>`_.
7286
7387 .. [1] https://docs.python.org/3/library/pickle.html
7488
@@ -117,10 +131,6 @@ def to_pickle(
117131
118132
119133@set_module ("pandas" )
120- @doc (
121- storage_options = _shared_docs ["storage_options" ],
122- decompression_options = _shared_docs ["decompression_options" ] % "filepath_or_buffer" ,
123- )
124134def read_pickle (
125135 filepath_or_buffer : FilePath | ReadPickleBuffer ,
126136 compression : CompressionOptions = "infer" ,
@@ -140,10 +150,34 @@ def read_pickle(
140150 String, path object (implementing ``os.PathLike[str]``), or file-like
141151 object implementing a binary ``readlines()`` function.
142152 Also accepts URL. URL is not limited to S3 and GCS.
143-
144- {decompression_options}
145-
146- {storage_options}
153+ compression : str or dict, default 'infer'
154+ For on-the-fly decompression of on-disk data. If 'infer' and 'filepath_or_buffer' is
155+ path-like, then detect compression from the following extensions: '.gz',
156+ '.bz2', '.zip', '.xz', '.zst', '.tar', '.tar.gz', '.tar.xz' or '.tar.bz2'
157+ (otherwise no compression).
158+ If using 'zip' or 'tar', the ZIP file must contain only one data file to be read in.
159+ Set to ``None`` for no decompression.
160+ Can also be a dict with key ``'method'`` set
161+ to one of {``'zip'``, ``'gzip'``, ``'bz2'``, ``'zstd'``, ``'xz'``, ``'tar'``} and
162+ other key-value pairs are forwarded to
163+ ``zipfile.ZipFile``, ``gzip.GzipFile``,
164+ ``bz2.BZ2File``, ``zstandard.ZstdDecompressor``, ``lzma.LZMAFile`` or
165+ ``tarfile.TarFile``, respectively.
166+ As an example, the following could be passed for Zstandard decompression using a
167+ custom compression dictionary:
168+ ``compression={'method': 'zstd', 'dict_data': my_compression_dict}``.
169+
170+ .. versionadded:: 1.5.0
171+ Added support for `.tar` files.
172+ storage_options : dict, optional
173+ Extra options that make sense for a particular storage connection, e.g.
174+ host, port, username, password, etc. For HTTP(S) URLs the key-value pairs
175+ are forwarded to ``urllib.request.Request`` as header options. For other
176+ URLs (e.g. starting with "s3://", and "gcs://") the key-value pairs are
177+ forwarded to ``fsspec.open``. Please see ``fsspec`` and ``urllib`` for more
178+ details, and for more examples on storage options refer `here
179+ <https://pandas.pydata.org/docs/user_guide/io.html?
180+ highlight=storage_options#reading-writing-remote-files>`_.
147181
148182 Returns
149183 -------
0 commit comments