How does chunked reading (chunksize) help with large files in read_csv?

Quick Overview

Asks how the chunksize option of pandas read_csv lets you process a CSV file that is larger than memory. Tests whether you can aggregate per chunk and combine the results correctly, and whether you know which operations and dtype issues make chunked reading harder.

How does chunked reading (chunksize) help with large files in read_csv?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

How does chunked reading (`chunksize`) help with large files in `read_csv`? For context, picture a 20 GB CSV of transactions that has to be summarized in pandas on a machine with 16 GB of RAM. ```hint Partial results Look at what `read_csv` returns once `chunksize` is set, then ask which summaries can be built from pieces of the file and combined later. ``` ### What a Strong Answer Covers - What `read_csv` returns when `chunksize` is set and how the result is consumed - Why peak memory follows the chunk size and not the file size - How to combine per-chunk results correctly for sums, counts, means and group-bys - Which operations do not split cleanly across chunks, and the options for them - Practical pitfalls such as per-chunk dtype inference and collecting every chunk back into memory ### Follow-up Questions - How would you compute the mean and the standard deviation of a column across chunks in a single pass? - How would you get the exact median, or a good approximation, of a column in a file that does not fit in memory? - A column comes back as `int64` in some chunks and as `float64` or `object` in others. Why, and how do you prevent it? - When would you move from chunked pandas to a tool such as Dask, DuckDB or Polars?

Overview: Asks how the chunksize option of pandas read_csv lets you process a CSV file that is larger than memory. Tests whether you can aggregate per chunk and combine the results correctly, and whether you know which operations and dtype issues make chunked reading harder.

General logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

How does chunked reading (chunksize) help with large files in read_csv?

For context, picture a 20 GB CSV of transactions that has to be summarized in pandas on a machine with 16 GB of RAM.

What a Strong Answer Covers Guidance

  • What read_csv returns when chunksize is set and how the result is consumed
  • Why peak memory follows the chunk size and not the file size
  • How to combine per-chunk results correctly for sums, counts, means and group-bys
  • Which operations do not split cleanly across chunks, and the options for them
  • Practical pitfalls such as per-chunk dtype inference and collecting every chunk back into memory

Follow-up Questions Guidance

  • How would you compute the mean and the standard deviation of a column across chunks in a single pass?
  • How would you get the exact median, or a good approximation, of a column in a file that does not fit in memory?
  • A column comes back as int64 in some chunks and as float64 or object in others. Why, and how do you prevent it?
  • When would you move from chunked pandas to a tool such as Dask, DuckDB or Polars?