Pickle (serialize) object to file. Parameters ---------- path : str, path object, or file-like object String, path object (implementing ``os.PathLike[str]``), or file-like object implementing a binary ``write()`` function. File path where
(
self,
path: FilePath | WriteBuffer[bytes],
*,
compression: CompressionOptions = "infer",
protocol: int = pickle.HIGHEST_PROTOCOL,
storage_options: StorageOptions | None = None,
)
| 3052 | |
| 3053 | @final |
| 3054 | def to_pickle( |
| 3055 | self, |
| 3056 | path: FilePath | WriteBuffer[bytes], |
| 3057 | *, |
| 3058 | compression: CompressionOptions = "infer", |
| 3059 | protocol: int = pickle.HIGHEST_PROTOCOL, |
| 3060 | storage_options: StorageOptions | None = None, |
| 3061 | ) -> None: |
| 3062 | """ |
| 3063 | Pickle (serialize) object to file. |
| 3064 | |
| 3065 | Parameters |
| 3066 | ---------- |
| 3067 | path : str, path object, or file-like object |
| 3068 | String, path object (implementing ``os.PathLike[str]``), or file-like |
| 3069 | object implementing a binary ``write()`` function. File path where |
| 3070 | the pickled object will be stored. |
| 3071 | |
| 3072 | compression : str or dict, default 'infer' |
| 3073 | For on-the-fly compression of the output data. If 'infer' and |
| 3074 | 'path_or_buf' is path-like, then detect compression from the following |
| 3075 | extensions: '.gz', |
| 3076 | '.bz2', '.zip', '.xz', '.zst', '.tar', '.tar.gz', '.tar.xz' or '.tar.bz2' |
| 3077 | (otherwise no compression). |
| 3078 | Set to ``None`` for no compression. |
| 3079 | Can also be a dict with key ``'method'`` set to one of |
| 3080 | {``'zip'``, ``'gzip'``, ``'bz2'``, ``'zstd'``, ``'xz'``, ``'tar'``} and |
| 3081 | other key-value pairs are forwarded to |
| 3082 | ``zipfile.ZipFile``, ``gzip.GzipFile``, |
| 3083 | ``bz2.BZ2File``, ``zstandard.ZstdCompressor``, ``lzma.LZMAFile`` or |
| 3084 | ``tarfile.TarFile``, respectively. |
| 3085 | As an example, the following could be passed for faster compression and |
| 3086 | to create a reproducible gzip archive: |
| 3087 | ``compression={'method': 'gzip', 'compresslevel': 1, 'mtime': 1}``. |
| 3088 | |
| 3089 | protocol : int |
| 3090 | Int which indicates which protocol should be used by the pickler, |
| 3091 | default HIGHEST_PROTOCOL (see [1]_ paragraph 12.1.2). The possible |
| 3092 | values are 0, 1, 2, 3, 4, 5. A negative value for the protocol |
| 3093 | parameter is equivalent to setting its value to HIGHEST_PROTOCOL. |
| 3094 | |
| 3095 | .. [1] https://docs.python.org/3/library/pickle.html. |
| 3096 | |
| 3097 | storage_options : dict, optional |
| 3098 | Extra options that make sense for a particular storage connection, e.g. |
| 3099 | host, port, username, password, etc. For HTTP(S) URLs the key-value pairs |
| 3100 | are forwarded to ``urllib.request.Request`` as header options. For other |
| 3101 | URLs (e.g. starting with "s3://", and "gcs://") the key-value pairs are |
| 3102 | forwarded to ``fsspec.open``. Please see ``fsspec`` and ``urllib`` for more |
| 3103 | details, and for more examples on storage options refer `here |
| 3104 | <https://pandas.pydata.org/docs/user_guide/io.html? |
| 3105 | highlight=storage_options#reading-writing-remote-files>`_. |
| 3106 | |
| 3107 | See Also |
| 3108 | -------- |
| 3109 | read_pickle : Load pickled pandas object (or any object) from file. |
| 3110 | DataFrame.to_hdf : Write DataFrame to an HDF5 file. |
| 3111 | DataFrame.to_sql : Write DataFrame to a SQL database. |