Restore locally serialized pandas data
Persisting a DataFrame between Python processes or runs.
Pandas · Function
Loads a pickled pandas object—or any other pickled object—from a file, URL, or binary stream. It can infer or apply compression and accepts storage-specific connection options.
The function opens a pickle source supplied as a path, URL, or binary file-like object, optionally decompresses it, and returns the object reconstructed from the serialized data. Compression can be inferred from supported filename suffixes or selected explicitly, while storage options configure remote access.
pandas.read_pickle(filepath_or_buffer, compression='infer', storage_options=None)| Name | Type | Description |
|---|---|---|
filepath_or_buffer | str, path object, or file-like object | String, path-like object, or binary file-like object containing the pickle; URLs are also accepted. |
compression | str or dict, default 'infer' | Compression mode or mapping for on-the-fly decompression; defaults to inferring compression from a path suffix. |
storage_options | dict, optional | Additional options for the selected storage connection. |
object — the unpickled pandas object or any other object stored in the file.
import pandas as pd
frame = pd.DataFrame({"score": [10, 20]})
frame.to_pickle("scores.pkl")
restored = pd.read_pickle("scores.pkl")
print(restored)
Serializes a DataFrame, then loads it back from the pickle file.
Persisting a DataFrame between Python processes or runs.
Reading compressed pickle files without manually opening a decompressor.
Accessing pickle data through supported remote storage URLs.
It accepts a string, path-like object, or binary file-like object, and URLs are supported as well. The return value is the object stored in the pickle, not necessarily a DataFrame.
Follow-up: How would you load a compressed remote object? Pass the URL, select or infer compression, and provide any connection-specific settings through storage_options.
With compression='infer', pandas detects compression from supported suffixes when the input is path-like. It supports gzip, bz2, zip, xz, zstd, tar, and several tar-compressed suffixes; use None to disable decompression.
Follow-up: What constraint applies to archive inputs? A ZIP or TAR archive must contain only one data file.
storage_options is a dictionary for connection-specific settings such as host, port, username, or password. The options are used by the relevant URL or storage backend when opening the source.
Follow-up: Why should storage_options not be treated as a pickle-security mechanism? It configures access, but it does not make untrusted serialized data safe to unpickle.
pandas.DataFrame.to_picklepandas.Series.to_picklepandas.read_csv