Use read_pickle() to restore a trusted saved object, then validate its type and fields.

import pandas as pd
import pickle
import tempfile
import sqlite3
from pathlib import Path
from io import BytesIO, StringIO
df = pd.DataFrame({"NAME": ["Ravi", "Raju", "Alex"], "ID": [1, 2, 3],
"MATH": [30, 40, 50], "ENGLISH": [20, 30, 40]})
print(df)
with tempfile.TemporaryDirectory(dir=Path.cwd()) as folder:
target = Path(folder) / "my_data.pkl"
df.to_pickle(target)
my_data = pd.read_pickle(target)
pd.testing.assert_frame_equal(my_data, df)
print("Restored rows:", len(my_data))Expected output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
Restored rows: 3Path(r"D:\my_data") / "my_data.pkl" preserves backslashes. Check that the path points to a trusted file before unpickling.with tempfile.TemporaryDirectory(dir=Path.cwd()) as folder:
target = Path(folder) / "archive" / "my_data.pkl"
target.parent.mkdir()
df.to_pickle(target)
restored = pd.read_pickle(target)
pd.testing.assert_frame_equal(restored, df)
print("Read archive/my_data.pkl")Expected output
Read archive/my_data.pklwith tempfile.TemporaryDirectory(dir=Path.cwd()) as folder:
target = Path(folder) / "my_data.pkl.gz"
df.to_pickle(target)
restored = pd.read_pickle(target)
pd.testing.assert_frame_equal(restored, df)
print("Gzip restored rows:", len(restored))Expected output
Gzip restored rows: 3with tempfile.TemporaryDirectory(dir=Path.cwd()) as folder:
target = Path(folder) / "my_data.pkl"
df.to_pickle(target, compression="zip")
restored = pd.read_pickle(target, compression="zip")
pd.testing.assert_frame_equal(restored, df)
print("Explicit ZIP restored rows:", len(restored))Expected output
Explicit ZIP restored rows: 3buffer = BytesIO()
df.to_pickle(buffer)
buffer.seek(0)
restored = pd.read_pickle(buffer, compression=None)
pd.testing.assert_frame_equal(restored, df)
print("Buffer restored rows:", len(restored))Expected output
Buffer restored rows: 3series = pd.Series([30, 40, 50], index=[1, 2, 3], name="MATH")
with tempfile.TemporaryDirectory(dir=Path.cwd()) as folder:
target = Path(folder) / "marks.pkl"
series.to_pickle(target)
restored = pd.read_pickle(target)
assert isinstance(restored, pd.Series)
pd.testing.assert_series_equal(restored, series)
print("Stored type:", type(restored).__name__)
print(restored)Expected output
Stored type: Series
1 30
2 40
3 50
Name: MATH, dtype: int64typed = pd.DataFrame({"units": pd.array([2, None], dtype="Int64"),
"state": pd.Categorical(["ready", "review"])},
index=pd.Index(["001", "002"], name="order_id"))
with tempfile.TemporaryDirectory(dir=Path.cwd()) as folder:
target = Path(folder) / "orders.pkl"
typed.to_pickle(target)
restored = pd.read_pickle(target)
pd.testing.assert_frame_equal(restored, typed)
print(restored.dtypes)
assert restored.index.tolist() == ["001", "002"]
assert restored["units"].isna().sum() == 1Expected output
units Int64
state category
dtype: objectsales = pd.DataFrame({"id": ["001", "002", "003"], "amount": ["5.25", "20", "pending"]})
with tempfile.TemporaryDirectory(dir=Path.cwd()) as folder:
target = Path(folder) / "sales.pkl"
sales.to_pickle(target)
raw = pd.read_pickle(target)
assert isinstance(raw, pd.DataFrame) and raw.columns.tolist() == ["id", "amount"]
clean = raw.copy()
clean["amount"] = pd.to_numeric(raw["amount"], errors="coerce")
valid = clean["amount"].ge(0).fillna(False)
accepted = clean.loc[valid].copy()
review = raw.loc[~valid].copy()
print("Accepted total:", accepted["amount"].sum())
print("Review IDs:", review["id"].tolist())
assert accepted["amount"].sum() == 25.25
assert len(accepted) + len(review) == len(raw)Expected output
Accepted total: 25.25
Review IDs: ['003']| Parameter | Purpose |
|---|---|
filepath_or_buffer | Required path or binary readable buffer. |
compression | infer by default; specify the format when the suffix does not identify it. |
storage_options | Forwarded options for supported storage backends. |
Trusted sources only: loading a pickle can execute code. Do not load an unknown attachment merely to inspect it. Missing file: check the working directory and source path. Wrong compression: match the writer settings. Compatibility errors: recreate the documented Python/Pandas environment or regenerate an interchange file in the producing environment. Pickle is not a permanent cross-version guarantee. Large files: read_pickle() loads an object and has no chunksize parameter. Consider chunked CSV or SQL imports for large workflows. See Python pickle and database pickling.
with tempfile.TemporaryDirectory(dir=Path.cwd()) as folder:
target = Path(folder) / "exercise.pkl"
df.to_pickle(target)
restored = pd.read_pickle(target)
assert isinstance(restored, pd.DataFrame)
answer = restored.loc[restored["MATH"].ge(40), ["NAME", "MATH"]]
print(answer)
assert answer["NAME"].tolist() == ["Raju", "Alex"]Expected output
NAME MATH
1 Raju 40
2 Alex 50Open in Google Colab View on GitHub
Run examples in order. The notebook creates all practice files. Save a copy to keep edits.
Reference: Pandas read_pickle documentation.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.