Pickle (serialize) object to file. Parameters ---------- obj : any object Any python object. filepath_or_buffer : str, path object, or file-like object String, path object (implementing ``os.PathLike[str]``), or file-like object implementing a binary ``w
(
obj: Any,
filepath_or_buffer: FilePath | WriteBuffer[bytes],
compression: CompressionOptions = "infer",
protocol: int = pickle.HIGHEST_PROTOCOL,
storage_options: StorageOptions | None = None,
)
| 31 | |
| 32 | @set_module("pandas") |
| 33 | def to_pickle( |
| 34 | obj: Any, |
| 35 | filepath_or_buffer: FilePath | WriteBuffer[bytes], |
| 36 | compression: CompressionOptions = "infer", |
| 37 | protocol: int = pickle.HIGHEST_PROTOCOL, |
| 38 | storage_options: StorageOptions | None = None, |
| 39 | ) -> None: |
| 40 | """ |
| 41 | Pickle (serialize) object to file. |
| 42 | |
| 43 | Parameters |
| 44 | ---------- |
| 45 | obj : any object |
| 46 | Any python object. |
| 47 | filepath_or_buffer : str, path object, or file-like object |
| 48 | String, path object (implementing ``os.PathLike[str]``), or file-like |
| 49 | object implementing a binary ``write()`` function. |
| 50 | Also accepts URL. URL has to be of S3 or GCS. |
| 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}``. |
| 66 | protocol : int |
| 67 | Int which indicates which protocol should be used by the pickler, |
| 68 | default HIGHEST_PROTOCOL (see [1], paragraph 12.1.2). The possible |
| 69 | values for this parameter depend on the version of Python. For Python |
| 70 | 2.x, possible values are 0, 1, 2. For Python>=3.0, 3 is a valid value. |
| 71 | For Python >= 3.4, 4 is a valid value. A negative value for the |
| 72 | protocol parameter is equivalent to setting its value to |
| 73 | HIGHEST_PROTOCOL. |
| 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>`_. |
| 83 | |
| 84 | .. [1] https://docs.python.org/3/library/pickle.html |
| 85 | |
| 86 | See Also |
| 87 | -------- |
| 88 | read_pickle : Load pickled pandas object (or any object) from file. |
| 89 | DataFrame.to_hdf : Write DataFrame to an HDF5 file. |
| 90 | DataFrame.to_sql : Write DataFrame to a SQL database. |