How to Analyze Parquet Files with DuckDB in Python
To analyze Parquet with DuckDB in Python, install the library with pip install duckdb, open a connection and put the file path straight into the FROM clause of a SQL query. DuckDB runs inside the Python process itself, with no server, and returns the result as a pandas DataFrame when you ask for one. The file never has to be loaded whole into memory: the query reads only the columns and rows the SELECT asks for.
What do you need to install to query Parquet with DuckDB?
The install is one line. DuckDB is a Python library, not a database that needs a server, a port or a configured user.
After installing, import the library and create a connection. In a notebook the shortest form is duckdb.sql(), which uses an in-memory connection. If you prefer to control the lifecycle, use duckdb.connect() and close it at the end.
All the work happens on the local machine. The file path is the same one you would pass to pandas.read_parquet, and the result comes back in a format the rest of your code already knows.
- pip install duckdb
- import duckdb
- con = duckdb.connect() # in-memory connection
- duckdb.sql("SELECT 1") # quick smoke test
How do you run your first query on a Parquet file?
With the connection open, the file path goes straight into the FROM clause, in single quotes. DuckDB recognises the .parquet extension and reads the file as if it were a table.
Start with SELECT * plus a LIMIT to check the columns and the shape of the data. The .df() at the end turns the result into a pandas DataFrame, the most comfortable format to inspect in a notebook.
The read_parquet() function does the same thing explicitly. It is useful when you want the code to make clear that the source is a file and not a database table.
To get the row count without pulling rows, use COUNT(*). To see column names and types, use DESCRIBE SELECT * FROM 'file.parquet'.
- duckdb.sql("SELECT * FROM 'sales.parquet' LIMIT 5").df()
- duckdb.sql("SELECT COUNT(*) FROM read_parquet('sales.parquet')").fetchone()
- duckdb.sql("DESCRIBE SELECT * FROM 'sales.parquet'").df()
How do you filter and aggregate before pulling data into pandas?
The point of using SQL here is to push the work into the query. Write the WHERE, GROUP BY and ORDER BY in SQL and bring into pandas only the slice you care about.
That skips the intermediate step of loading everything, converting it and filtering afterwards. The DataFrame that arrives is already the answer to the question.
DuckDB also accepts wildcards in the path. A pattern like 'data/*.parquet' reads every file in the folder as a single table, which handles the common case of data partitioned by month or by day.
If the files sit in subfolders named key=value, DuckDB uses that structure and exposes the keys as columns. Run DESCRIBE before writing the query to confirm what is there.
- duckdb.sql("SELECT category, SUM(amount) AS total FROM 'sales.parquet' WHERE year = 2024 GROUP BY category ORDER BY total DESC").df()
- duckdb.sql("SELECT * FROM 'data/*.parquet' LIMIT 10").df()
- List the columns in the SELECT instead of using * when the file is wide
How do you use the query result in the rest of your Python code?
.df() returns a pandas DataFrame. From there everything you already do applies: charts, joins with other tables, exports.
The .arrow() method returns an Arrow table, handy when the next step is another library that speaks the same format. The .fetchall() method returns a list of tuples, practical for unique values or a single record.
The reverse direction works too. You can register a DataFrame already in memory as a view and query it with SQL alongside the Parquet file, joining the large on-disk base with a small lookup table.
In pipelines the pattern is: query in DuckDB, result in a DataFrame, transformation in pandas. Each tool does what it does best. If the pandas side of that pipeline is still new to you, the course at python.paanalytics.net covers pandas, NumPy, data cleaning and a final end-to-end project, so the step after the query is not a black box.
- df = duckdb.sql("SELECT * FROM 'sales.parquet' WHERE amount > 100").df()
- con.register('lookup', small_df)
- duckdb.sql("SELECT * FROM 'sales.parquet' s JOIN lookup l ON s.id = l.id").df()
DuckDB or pandas: which one should read the Parquet file?
Both read Parquet and both run in the same Python process. The difference is where the filtering happens.
pandas.read_parquet brings the selected columns into a DataFrame and you filter in pandas afterwards. DuckDB runs the SQL first and returns only the result rows. For a quick look at a small file, either works. For a file that does not fit comfortably in memory, the SQL-first route avoids a step you would have to undo.
The two are not exclusive. Registering a DataFrame as a view lets you mix a small in-memory table with a large Parquet file in one query.
- Use pandas when the file is small and you want the DataFrame immediately
- Use DuckDB when you want the filter and the aggregation to happen before the data reaches pandas
- Use both when a large Parquet file has to be joined with a small in-memory table
What mistakes should you avoid when querying Parquet with DuckDB?
The most frequent mistake is calling .df() on a huge result. The query itself is light, but the DataFrame is not: it materialises every row you selected. Keep the LIMIT while exploring and only drop it when the result is genuinely small.
The second is forgetting the quotes around the path. Without them, DuckDB reads the name as an identifier and the query fails.
The third is assuming the file carries no schema. Parquet stores types and DuckDB respects them. If a date column arrives as text, check how the file was written before casting inside the query.
Finally, close connections you opened with duckdb.connect() in scripts that run for a long time. In a notebook the in-memory connection is fine, but in a scheduled job an explicit close keeps things tidy.
- Keep a LIMIT on exploratory queries
- Always quote the file path in the FROM clause
- Run DESCRIBE before casting columns
- Close explicit connections in long-running scripts
| Task | pandas.read_parquet | DuckDB SQL |
|---|---|---|
| Inspect a small file | Reads the columns into a DataFrame | Runs the query and returns only the result |
| Select a few columns from a wide file | Use the columns= argument | List the columns in the SELECT |
| Aggregate before analysis | Load, then groupby | GROUP BY in the query, .df() on the result |
| Read a folder of partitioned files | Point at the folder or loop over the files | Wildcard in the FROM path |
| Join with a small in-memory table | Merge after loading | Register the DataFrame as a view and JOIN |
Comparison based on the code patterns shown in this tutorial; both tools run in the same Python process and neither needs a server.
Start from zero and finish with an end-to-end machine learning project
Secure payment via Hotmart, up to 12 installments.
Enroll now Product siteFrequently asked questions
Does DuckDB need a server running in the background?
No. DuckDB is a library that runs inside the Python process. You install it with pip, import it and query; there is no service to start, no port to open and no credentials to configure.
Can I query a whole folder of Parquet files instead of one file?
Yes. Put a wildcard in the path, as in 'data/*.parquet', and DuckDB reads the files as a single table. If the folder is partitioned in key=value subfolders, the keys appear as columns.
Does .df() load the entire file into memory?
No. The query runs first and only the result set is converted into a DataFrame. What lands in memory is the rows and columns your SELECT returned, not the file.
Can I join a Parquet file with a pandas DataFrame already in memory?
Yes. Register the DataFrame as a view with con.register() and reference it in the JOIN next to the Parquet path. The large base stays on disk and the small table stays in memory.
Do I need to know SQL before starting?
You only need the four clauses used here: SELECT, FROM, WHERE and GROUP BY. The course starts from zero and does not require prior programming experience, so SQL is not a gate you have to pass first.