Start learning free

LESSON 6 OF 107 · DATA ENGINEERING: FILES AND FORMATS

Folders by Date in Python

Practice

Big tables are often saved in pieces: one folder per value of a column, usually the date. This is called partitioning. A report about one day then opens one folder and skips all the others. Without it, every question about one day reads the whole history, and that history grows every day the shop is open.

pandas does it with partition_cols= in to_parquet. You give a folder name instead of a file name, and pandas writes one sub-folder per value, named column=value. The column is left out of the files inside, because the folder name already holds it. Here is the idea on another column, status:

orders.to_parquet("by_status", partition_cols=["status"], index=False)
print(sorted(os.listdir("by_status")))   # ['status=cancelled', 'status=paid']
paid = pd.read_parquet("by_status", filters=[("status", "=", "paid")])

os.listdir(folder) gives the names inside a folder in no fixed order, so you wrap it in sorted. filters= in pd.read_parquet(folder, filters=[…]) is a list of conditions, each a tuple (column, "=", value), and only the folders whose names match are opened. The column comes back in the result, rebuilt from the folder names.

Your program prints one folder name per day that had orders. Then the check looks inside one folder and at the day you read back:

date=2026-05-01
date=2026-05-02
date=2026-05-03
date=2026-05-04
date=2026-05-06
date=2026-05-07
files in date=2026-05-03: 1
columns inside it: ['order_id', 'customer_id', 'product', 'category', 'quantity', 'unit_price', 'status']
rows read for one day: 6
dates in them: ['2026-05-03']

Your task: below week, write it to the folder orders_by_day partitioned by date, print the names inside that folder in sorted order, and read only the day 2026-05-03 back into day.


Assignment
Follow the Coding Tutorial to write week as one folder per date, print the folder names in order and read one day back with filters=.


Hint
Both to_parquet and read_parquet take the folder name "orders_by_day", not a file. filters= is a list of tuples, so the round brackets go inside square ones, and the date is the text "2026-05-03".


STEP 1 OF 4
    Ctrl + Enter to check

    De Partition Folders in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free