Ask a CSV file for the closing price of one stock and it reads every byte of every column of every row. Ask a Parquet file for the same thing and it reads the bytes of one column. That single difference, storing a table by column instead of by row, is why research data lives in Parquet.
The idea in plain words
Where we are. The first topic of the Parquet skill. It builds on the DuckDB skill, where Parquet was just a source to query, and opens the file itself. The next topics look inside it (row groups, pages, codecs) and then use that layout to read less.
Storing a table one column after another instead of one record after another. A query that needs two columns reads two columns, and because each column holds one kind of value, it compresses far better than rows of mixed values.
Same table, two files
load_bars() returns the daily bars of five stocks as an Arrow table of about 33,000 rows. pyarrow.csv.write_csv and pyarrow.parquet.write_table write it both ways. The Parquet file is already smaller, and that is with the default settings, before any tuning.
# Write the table as CSV and as Parquet, then compare the two filesprint(table.num_rows, "rows;", table.column_names)csv_bytes, parquet_bytes = file_sizes(table, ".")print("csv bytes:", csv_bytes)print("parquet bytes:", parquet_bytes)print("csv is", round(csv_bytes / parquet_bytes, 2), "times bigger")
33493 rows; ['symbol', 'date', 'open', 'high', 'low', 'close', 'volume'] csv bytes: 1914722 parquet bytes: 1246066 csv is 1.54 times bigger
Read only what you need
The size win is real, the speed win is bigger. A Parquet file ends with a footer that records where every column's bytes start and how many there are. read_columns asks for two columns and the reader jumps straight to them. column_bytes reads only that footer to tell you what the jump costs, without touching the data.
# Read two columns, then ask the footer how much of the file they taketwo = read_columns("bars.parquet", ["date", "close"])print(two.column_names, two.num_rows)asked = column_bytes("bars.parquet", ["date", "close"])whole = column_bytes("bars.parquet", table.column_names)print("bytes for date + close:", asked, "of", whole)print("share of the file:", f"{asked / whole:.0%}")
['date', 'close'] 33493 bytes for date + close: 311253 of 1244525 share of the file: 25%
# Which single column is the heaviest in the file?for name in table.column_names: print(name, column_bytes("bars.parquet", [name]))print("widest:", widest_column("bars.parquet"))
symbol 161
date 81455
open 226085
high 226308
low 230269
close 229798
volume 250449
widest: ('volume', 250449)This table has seven columns and a query usually wants two or three. A feature table with two hundred columns and a model that uses ten is where columnar storage turns a minutes-long read into seconds. Keep wide tables in Parquet and ask for columns by name.
What the tasks ask
- 1`file_sizes`Write
bars.csvwithpacsv.write_csvandbars.parquetwithpq.write_table; return both sizes fromos.path.getsize. - 2`read_columns`
pq.read_table(path, columns=...): the reader fetches only those columns. - 3`column_bytes`Loop the row groups of
pq.ParquetFile(path).metadataand addtotal_compressed_sizeof the wanted column chunks. - 4`widest_column`Measure each column with
column_bytesand return the largest as(name, bytes); a tie goes to the first.
date and close from a Parquet file with seven columns. Roughly how much of the file does a good reader fetch?Lab · what's graded
4 tasks · 100 pts- Write it both ways:
file_sizes25 pts - Read only the columns you need:
read_columns25 pts - Count bytes from the footer:
column_bytes25 pts - Find the widest column:
widest_column25 pts