Skip to content
ParquetKitGitHub
On this page

Guides

fastparquet Categorical Columns: Using the categories Argument

Why categoricals matter here

A column like country or status holds a handful of distinct strings repeated millions of times. Parquet usually stores it with dictionary encoding: the distinct values once, then small integer codes. pandas has the same idea in the category dtype. If the reader maps one onto the other, you skip decoding millions of Python strings, and the DataFrame uses a fraction of the memory.

fastparquet can do this mapping directly, but only when it knows which columns to treat as categorical.

Check whether a column is dictionary-encoded

Before tuning anything, confirm the encoding. In DuckDB:

SELECT path_in_schema, encodings
FROM parquet_metadata('events.parquet');
-- country | PLAIN, RLE, RLE_DICTIONARY

RLE_DICTIONARY (or PLAIN_DICTIONARY in older files) means the column is a good candidate. You can run the same query in the SQL Workbench without installing anything. The file stays in your browser.

Files written by fastparquet

When you write a category column with fastparquet, it stores pandas metadata alongside the data. On read, fastparquet uses that metadata to restore the dtype automatically:

import fastparquet

pf = fastparquet.ParquetFile("events.parquet")
print(pf.categories)                 # {'country': 2}
df = pf.to_pandas()
print(df["country"].cat.categories)  # Index(['JP', 'US'])

Don't rely on df.dtypes for this check. Recent versions print a placeholder categories=range(0, 2) there, while .cat.categories shows the real labels.

Files written by anything else

Spark, DuckDB and pyarrow files carry no fastparquet metadata, so pf.categories is empty and you get plain strings. Ask for categoricals explicitly:

df = pf.to_pandas(categories=["country"])
# or give an upper bound on the number of labels per column
df = pf.to_pandas(categories={"country": 300})

The dict form tells fastparquet how many labels to allocate. Without it, fastparquet reserves a large default range and fills in the real labels as it reads. Compare memory before and after to see whether it was worth it:

df.memory_usage(deep=True)

For low-cardinality columns the categorical version is usually many times smaller. For a column with mostly unique values, such as an ID or a free-text field, a categorical is slower and larger. Leave those as strings.

Categoricals get lost between engines

The category dtype is a pandas concept. Parquet only records that the column is a string that happens to be dictionary-encoded. In testing with current releases:

  • fastparquet writes, fastparquet reads: category is restored.
  • pyarrow writes, pyarrow reads: category is restored.
  • fastparquet writes, pyarrow reads: plain strings.

If your pipeline mixes engines, don't count on the round trip. With pyarrow, request the dictionary explicitly:

import pyarrow.parquet as pq
df = pq.read_table("events.parquet", read_dictionary=["country"]).to_pandas()

Or call df["country"].astype("category") after reading, whatever the engine.

Concatenating several files

Each file has its own dictionary. When you pd.concat two frames whose categories differ, pandas falls back to a plain string column with no warning. Use union_categoricals to keep the dtype:

from pandas.api.types import union_categoricals
merged = union_categoricals([a["country"], b["country"]])

If you only need the combined file, merging at the Parquet level avoids the problem entirely. See merging multiple Parquet files.

A note on the engine's future

Recent pandas releases emit a deprecation warning for engine="fastparquet". Code that calls fastparquet.ParquetFile directly is unaffected, but new projects should plan on pyarrow and read_dictionary. The fastparquet vs pyarrow comparison covers the remaining differences. If all you want is to see which labels a column holds, run SELECT country, count(*) FROM 'events.parquet' GROUP BY 1 in the SQL Workbench instead, or open the file in the Parquet Viewer.

Frequently asked questions

What does the categories argument in fastparquet to_pandas do?
It tells fastparquet to build a pandas Categorical straight from the column's Parquet dictionary pages instead of decoding every value to a string. Pass a list of column names, or a dict mapping names to the expected number of labels.
Why did my category column come back as plain strings?
The category dtype lives in pandas metadata, not in Parquet itself. A file written by fastparquet and read with the pyarrow engine comes back as strings; pass read_dictionary in pyarrow or call astype('category') after reading.
Does DuckDB keep the category type?
No. DuckDB reads dictionary-encoded columns as ordinary VARCHAR. Dictionary encoding is a storage detail in Parquet, so SQL engines see the decoded strings.

Related guides