Sub topic 13
Reading Files Back to Verify Them
This chapter assumes you can write CSV, Excel, and Parquet files with pandas and that you know the common dtypes (int, float, string, datetime). A file that wrote without an error can still hold different data than you meant to save. The check is simple: read the file back and compare it with the original.
Why verify
Each format keeps different things. A CSV file stores only text, so dtypes are lost. Excel keeps numbers and dates but has its own limits. Parquet keeps dtypes, but some objects cannot be stored. A read-back check tells you which of these changes happened to your data, before someone else finds it.
Compare with assert_frame_equal
pd.testing.assert_frame_equal compares two DataFrames. It checks values, dtypes, the index, and column names. If anything differs, it raises an AssertionError that names the difference. If everything matches, it returns nothing.
import pandas as pd
df = pd.DataFrame({
"order_id": [1, 2, 3],
"customer": ["Ana", "Ben", None],
"amount": [19.99, 5.5, None],
"ordered_at": pd.to_datetime(["2026-01-05", "2026-01-06", "2026-01-07"]),
})
df.to_parquet("orders.parquet", index=False)
restored = pd.read_parquet("orders.parquet")
pd.testing.assert_frame_equal(df, restored)
print("Parquet round trip OK")Parquet usually passes this check because it stores the dtypes along with the data.
Verifying a CSV
The same check on a CSV file usually fails, because CSV has no dtypes.
df.to_csv("orders.csv", index=False)
naive = pd.read_csv("orders.csv")
print(naive.dtypes)
try:
pd.testing.assert_frame_equal(df, naive)
except AssertionError as err:
print(err)The ordered_at column comes back as text, and the error message says the dtypes differ. Tell read_csv what the columns are, and compare again:
restored = pd.read_csv("orders.csv", parse_dates=["ordered_at"])
pd.testing.assert_frame_equal(df, restored)
print("CSV round trip OK")Here the check passes only because the read step restores the datetime column. The customer column was text with a missing value, and the amount column was float with a missing value. Both come back unchanged here, because empty fields in the CSV are read as missing values.
Common reasons a check fails
| Symptom | Likely cause | Fix |
|---|---|---|
| Datetime column read as `object` | CSV stores dates as text | Pass `parse_dates` to `read_csv` |
| Integer column read as `float64` | Missing values in a CSV integer column | Use the nullable `Int64` dtype on both sides |
| Leading zeros lost in codes | `read_csv` guessed a number | Pass `dtype={"code": str}` |
| Index appears as an extra column | File was written without `index=False` | Write with `index=False`, or pass `index_col=0` |
| Float values differ slightly | `float_format` rounded values on write | Compare with a tolerance (see below) |
Allowing small differences
If you rounded floats on write, an exact comparison fails by design. Set a tolerance with rtol or atol:
df.to_csv("orders_round.csv", index=False, float_format="%.2f")
restored = pd.read_csv("orders_round.csv", parse_dates=["ordered_at"])
pd.testing.assert_frame_equal(df, restored, atol=0.01)
print("Rounded CSV matches within 0.01")Use a tolerance only when you chose the loss yourself. A tolerance that is wider than your rounding can hide real errors.
Quick checks when an exact match is not the goal
An Excel file is often meant for people, with extra formatting or a different layout. A full comparison may not apply. Cheaper checks still catch most mistakes:
- Compare
df.shapewith the shape of the file you read back. - Compare the column names:
list(df.columns) == list(restored.columns). - Compare a key total, such as
df["amount"].sum().
Exercise
You write a DataFrame with an integer id column and a datetime created column to CSV with index=False. You read it back with pd.read_csv("file.csv") and assert_frame_equal fails. Which column causes the failure, and what argument fixes it?
Show answer
The created column fails, because it comes back as text (object). Pass parse_dates=["created"] to read_csv. The id column is read as an integer again, as long as it has no missing values.
Exercise
Why can assert_frame_equal pass for a Parquet round trip with no extra read arguments, but not for a CSV round trip?
Show answer
Parquet stores the dtypes in the file, so read_parquet restores them. CSV stores only text, so read_csv has to guess dtypes or be told them.