Sub topic 9
Saving a DataFrame to Parquet
This chapter assumes you can build and inspect a DataFrame, run Python scripts, install packages with pip, and read column dtypes. Earlier chapters covered CSV and Excel. Here you write the same data to Parquet and see what changes.
What Parquet is
Parquet is a binary file format that stores data by column and records the type of each column in the file. A CSV file is plain text, so every value is a string until a reader guesses its type. A Parquet file keeps integers, floats, booleans, and datetimes as those types. Parquet files are usually much smaller than the equivalent CSV, and they let a reader load only the columns it needs.
Install an engine
pandas does not read or write Parquet by itself. It calls a library, called an engine. Two engines work:
pyarrow, the usual choice and the more complete one.fastparquet, a lighter alternative.
Install one of them:
pip install pyarrowWith the default engine="auto", pandas tries pyarrow first and falls back to fastparquet. If neither is installed, to_parquet raises an ImportError.
Write and read a file
This example builds the same orders table used in earlier chapters, then writes and reads it back:
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)
back = pd.read_parquet("orders.parquet")
print(back.dtypes)The ordered_at column comes back as a datetime column. You did not pass parse_dates, which a CSV round trip would have required. The order_id column stays an integer, and amount stays a float with a missing value in it.
The index argument
By default index=None, and pandas decides what to do:
- A plain
RangeIndex(0, 1, 2, ...) is stored only as a small piece of metadata, not as a column. - Any other index is stored as an extra column.
Set index=False to drop the index, as the example does. Set index=True to always store it. If your index carries meaning, such as a customer ID, keep it. If it is just row numbers, drop it.
Column names must be strings
Parquet requires column names to be strings. If your DataFrame has integer column names, the pyarrow engine raises an error or converts them. Convert them yourself before writing, so the result is predictable:
df.columns = df.columns.astype(str)Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `ImportError` mentioning `pyarrow` or `fastparquet` | No engine installed | Run `pip install pyarrow` |
| `ArrowInvalid` or `ArrowTypeError` on write | A column of mixed types, such as numbers and strings in one object column | Convert the column to one type first |
| Extra column named `index` or `__index_level_0__` after reading | The index was stored as a column | Pass `index=False` when writing |
Exercise
Exercise
You write a DataFrame with a DatetimeIndex named "orderedat" using df.toparquet("out.parquet") and no other arguments. Is the index stored in the file, and how would you change the call to leave it out?
Show answer
Yes. Only a plain RangeIndex is stored as metadata. A DatetimeIndex is not a RangeIndex, so the default index=None stores it as an extra column. Pass index=False, as in df.to_parquet("out.parquet", index=False), to leave it out.
Exercise
A teammate says their to_parquet call fails with an ImportError. What is the most likely cause, and what single command fixes it?
Show answer
No Parquet engine is installed. pandas needs pyarrow or fastparquet. Running pip install pyarrow fixes it.