Search Shortcut cmd + k | ctrl + k

- Installation
- Documentation

- Getting Started
- Connect

- Overview
- Concurrency

- Data Import

- Overview
- Data Sources
- CSV Files

- Overview
- Auto Detection
- Reading Faulty CSV Files
- Tips

- JSON Files

- Overview
- Creating JSON
- Loading JSON
- Writing JSON
- JSON Type
- JSON Functions
- Format Settings
- Installing and Loading
- SQL to / from JSON
- Caveats

- Multiple Files

- Overview
- Combining Schemas

- Parquet Files

- Overview
- Metadata
- Encryption
- Tips

- Partitioning

- Hive Partitioning
- Partitioned Writes

- Appender
- INSERT Statements

- Client APIs

- Overview
- C

- Overview
- Startup
- Configuration
- Query
- Data Chunks
- Vectors
- Values
- Types
- Prepared Statements
- Appender
- Table Functions
- Replacement Scans
- API Reference

- C++
- CLI

- Overview
- Arguments
- Dot Commands
- Output Formats
- Editing
- Autocomplete
- Syntax Highlighting

- Dart
- Go
- Java
- Julia
- Node.js (Neo)

- Overview

- Node.js

- Overview
- API Reference

- Python

- Overview
- Data Ingestion
- Conversion between DuckDB and Python
- DB API
- Relational API
- Function API
- Types API
- Expression API
- Spark API
- API Reference
- Known Python Issues

- R
- Rust
- Swift
- Wasm

- Overview
- Instantiation
- Data Ingestion
- Query
- Extensions

- ADBC
- ODBC

- Overview
- Linux Setup
- Windows Setup
- macOS Setup
- Configuration

- SQL

- Introduction
- Statements

- Overview
- ANALYZE
- ALTER TABLE
- ALTER VIEW
- ATTACH and DETACH
- CALL
- CHECKPOINT
- COMMENT ON
- COPY
- CREATE INDEX
- CREATE MACRO
- CREATE SCHEMA
- CREATE SECRET
- CREATE SEQUENCE
- CREATE TABLE
- CREATE VIEW
- CREATE TYPE
- DELETE
- DESCRIBE
- DROP
- EXPORT and IMPORT DATABASE
- INSERT
- LOAD / INSTALL
- PIVOT
- Profiling
- SELECT
- SET / RESET
- SET VARIABLE
- SUMMARIZE
- Transaction Management
- UNPIVOT
- UPDATE
- USE
- VACUUM

- Query Syntax

- SELECT
- FROM and JOIN
- WHERE
- GROUP BY
- GROUPING SETS
- HAVING
- ORDER BY
- LIMIT and OFFSET
- SAMPLE
- Unnesting
- WITH
- WINDOW
- QUALIFY
- VALUES
- FILTER
- Set Operations
- Prepared Statements

- Data Types

- Overview
- Array
- Bitstring
- Blob
- Boolean
- Date
- Enum
- Interval
- List
- Literal Types
- Map
- NULL Values
- Numeric
- Struct
- Text
- Time
- Timestamp
- Time Zones
- Union
- Typecasting

- Expressions

- Overview
- CASE Statement
- Casting
- Collations
- Comparisons
- IN Operator
- Logical Operators
- Star Expression
- Subqueries

- Functions

- Overview
- Aggregate Functions
- Array Functions
- Bitstring Functions
- Blob Functions
- Date Format Functions
- Date Functions
- Date Part Functions
- Enum Functions
- Interval Functions
- Lambda Functions
- List Functions
- Map Functions
- Nested Functions
- Numeric Functions
- Pattern Matching
- Regular Expressions
- Struct Functions
- Text Functions
- Time Functions
- Timestamp Functions
- Timestamp with Time Zone Functions
- Union Functions
- Utility Functions
- Window Functions

- Constraints
- Indexes
- Meta Queries

- Information Schema
- Metadata Functions

- DuckDB's SQL Dialect

- Overview
- Indexing
- Friendly SQL
- Keywords and Identifiers
- Order Preservation
- PostgreSQL Compatibility

- Samples

- Configuration

- Overview
- Pragmas
- Secrets Manager

- Extensions

- Overview
- Core Extensions
- Community Extensions
- Working with Extensions
- Versioning of Extensions
- Arrow
- AutoComplete
- AWS
- Azure
- Delta
- Excel
- Full Text Search
- httpfs (HTTP and S3)

- Overview
- HTTP(S) Support
- Hugging Face
- S3 API Support
- Legacy Authentication Scheme for S3 API

- Iceberg
- ICU
- inet
- jemalloc
- MySQL
- PostgreSQL
- Spatial

- Overview
- Function Reference
- R-Tree Indexes
- GDAL Integration

- SQLite
- Substrait
- TPC-DS
- TPC-H
- VSS

- Guides

- Overview
- Data Viewers

- Tableau
- CLI Charting with YouPlot

- Database Integration

- Overview
- MySQL Import
- PostgreSQL Import
- SQLite Import

- File Formats

- Overview
- CSV Import
- CSV Export
- Directly Reading Files
- Excel Import
- Excel Export
- JSON Import
- JSON Export
- Parquet Import
- Parquet Export
- Querying Parquet Files

- Network and Cloud Storage

- Overview
- HTTP Parquet Import
- S3 Parquet Import
- S3 Parquet Export
- S3 Iceberg Import
- S3 Express One
- GCS Import
- Cloudflare R2 Import
- DuckDB over HTTPS / S3

- Meta Queries

- Describe Table
- EXPLAIN: Inspect Query Plans
- EXPLAIN ANALYZE: Profile Queries
- List Tables
- Summarize
- DuckDB Environment

- ODBC

- ODBC Guide

- Performance

- Overview
- Environment
- Import
- Schema
- Indexing
- Join Operations
- File Formats
- How to Tune Workloads
- My Workload Is Slow
- Benchmarks

- Python

- Installation
- Executing SQL
- Jupyter Notebooks
- SQL on Pandas
- Import from Pandas
- Export to Pandas
- Import from Numpy
- Export to Numpy
- SQL on Arrow
- Import from Arrow
- Export to Arrow
- Relational API on Pandas
- Multiple Python Threads
- Integration with Ibis
- Integration with Polars
- Using fsspec Filesystems

- SQL Editors

- DBeaver SQL IDE

- SQL Features

- AsOf Join
- Full-Text Search
- query and query_table Functions

- Snippets

- Create Synthetic Data
- Sharing Macros

- Glossary of Terms
- Browse Offline

- Operations Manual

- Overview
- Limits
- Non-Deterministic Behavior
- Embedding DuckDB
- DuckDB's Footprint

- Files Created by DuckDB
- Gitignore for DuckDB
- Reclaiming Space

- Securing DuckDB

- Overview
- Securing Extensions

- Development

- DuckDB Repositories
- Testing

- Overview
- sqllogictest Introduction
- Writing Tests
- Debugging
- Result Verification
- Persistent Testing
- Loops
- Multiple Connections
- Catch

- Profiling
- Release Calendar
- Building

- Overview
- Build Instructions
- Build Configuration
- Building Extensions
- Platforms
- Troubleshooting

- Benchmark Suite

- Internals

- Overview
- Storage Versions and Format
- Execution Format
- Pivot

- Sitemap
- Why DuckDB
- Media
- FAQ
- Code of Conduct
- Live Demo

Python API

## Installation

The DuckDB Python API can be installed using pip: `pip install duckdb`. Please see the installation page for details. It is also possible to install DuckDB using conda: `conda install python-duckdb -c conda-forge`.

Python version: DuckDB requires Python 3.7 or newer.

## Basic API Usage

The most straight-forward manner of running SQL queries using DuckDB is using the `duckdb.sql` command.

    import duckdb

    duckdb.sql("SELECT 42").show()

This will run queries using an in-memory database that is stored globally inside the Python module. The result of the query is returned as a Relation. A relation is a symbolic representation of the query. The query is not executed until the result is fetched or requested to be printed to the screen.

Relations can be referenced in subsequent queries by storing them inside variables, and using them as tables. This way queries can be constructed incrementally.

    import duckdb

    r1 = duckdb.sql("SELECT 42 AS i")
    duckdb.sql("SELECT i * 2 AS k FROM r1").show()

## Data Input

DuckDB can ingest data from a wide variety of formats – both on-disk and in-memory. See the data ingestion page for more information.

    import duckdb

    duckdb.read_csv("example.csv")                # read a CSV file into a Relation
    duckdb.read_parquet("example.parquet")        # read a Parquet file into a Relation
    duckdb.read_json("example.json")              # read a JSON file into a Relation

    duckdb.sql("SELECT * FROM 'example.csv'")     # directly query a CSV file
    duckdb.sql("SELECT * FROM 'example.parquet'") # directly query a Parquet file
    duckdb.sql("SELECT * FROM 'example.json'")    # directly query a JSON file

### DataFrames

DuckDB can directly query Pandas DataFrames, Polars DataFrames and Arrow tables. Note that these are read-only, i.e., editing these tables via `INSERT` or `UPDATE` statements is not possible.

#### Pandas

To directly query a Pandas DataFrame, run:

    import duckdb
    import pandas as pd

    pandas_df = pd.DataFrame({"a": [42]})
    duckdb.sql("SELECT * FROM pandas_df")

    ┌───────┐
    │   a   │
    │ int64 │
    ├───────┤
    │    42 │
    └───────┘

#### Polars

To directly query a Polars DataFrame, run:

    import duckdb
    import polars as pl

    polars_df = pl.DataFrame({"a": [42]})
    duckdb.sql("SELECT * FROM polars_df")

    ┌───────┐
    │   a   │
    │ int64 │
    ├───────┤
    │    42 │
    └───────┘

#### PyArrow

To directly query a PyArrow table, run:

    import duckdb
    import pyarrow as pa

    arrow_table = pa.Table.from_pydict({"a": [42]})
    duckdb.sql("SELECT * FROM arrow_table")

    ┌───────┐
    │   a   │
    │ int64 │
    ├───────┤
    │    42 │
    └───────┘

## Result Conversion

DuckDB supports converting query results efficiently to a variety of formats. See the result conversion page for more information.

    import duckdb

    duckdb.sql("SELECT 42").fetchall()   # Python objects
    duckdb.sql("SELECT 42").df()         # Pandas DataFrame
    duckdb.sql("SELECT 42").pl()         # Polars DataFrame
    duckdb.sql("SELECT 42").arrow()      # Arrow Table
    duckdb.sql("SELECT 42").fetchnumpy() # NumPy Arrays

## Writing Data to Disk

DuckDB supports writing Relation objects directly to disk in a variety of formats. The `COPY` statement can be used to write data to disk using SQL as an alternative.

    import duckdb

    duckdb.sql("SELECT 42").write_parquet("out.parquet") # Write to a Parquet file
    duckdb.sql("SELECT 42").write_csv("out.csv")         # Write to a CSV file
    duckdb.sql("COPY (SELECT 42) TO 'out.parquet'")      # Copy to a Parquet file

## Connection Options

Applications can open a new DuckDB connection via the `duckdb.connect()` method.

### Using an In-Memory Database

When using DuckDB through `duckdb.sql()`, it operates on an in-memory database, i.e., no tables are persisted on disk. Invoking the `duckdb.connect()` method without arguments returns a connection, which also uses an in-memory database:

    import duckdb

    con = duckdb.connect()
    con.sql("SELECT 42 AS x").show()

### Persistent Storage

The `duckdb.connect(dbname)` creates a connection to a persistent database. Any data written to that connection will be persisted, and can be reloaded by reconnecting to the same file, both from Python and from other DuckDB clients.

    import duckdb

    # create a connection to a file called 'file.db'
    con = duckdb.connect("file.db")
    # create a table and load data into it
    con.sql("CREATE TABLE test (i INTEGER)")
    con.sql("INSERT INTO test VALUES (42)")
    # query the table
    con.table("test").show()
    # explicitly close the connection
    con.close()
    # Note: connections also closed implicitly when they go out of scope

You can also use a context manager to ensure that the connection is closed:

    import duckdb

    with duckdb.connect("file.db") as con:
        con.sql("CREATE TABLE test (i INTEGER)")
        con.sql("INSERT INTO test VALUES (42)")
        con.table("test").show()
        # the context manager closes the connection automatically

### Configuration

The `duckdb.connect()` accepts a `config` dictionary, where configuration options can be specified. For example:

    import duckdb

    con = duckdb.connect(config = {'threads': 1})

### Connection Object and Module

The connection object and the `duckdb` module can be used interchangeably – they support the same methods. The only difference is that when using the `duckdb` module a global in-memory database is used.

> If you are developing a package designed for others to use, and use DuckDB in the package, it is recommend that you create connection objects instead of using the methods on the `duckdb` module. That is because the `duckdb` module uses a shared global database – which can cause hard to debug issues if used from within multiple different packages.

### Using Connections in Parallel Python Programs

The `DuckDBPyConnection` object is not thread-safe. If you would like to write to the same database from multiple threads, create a cursor for each thread with the `DuckDBPyConnection.cursor()` method.

## Loading and Installing Extensions

DuckDB's Python API provides functions for installing and loading extensions, which perform the equivalent operations to running the `INSTALL` and `LOAD` SQL commands, respectively. An example that installs and loads the `spatial` extension looks like follows:

    import duckdb

    con = duckdb.connect()
    con.install_extension("spatial")
    con.load_extension("spatial")

### Community Extensions

To load community extensions, use `repository="community"` argument to the `install_extension` method.

For example, install and load the `h3` community extension as follows:

    import duckdb

    con = duckdb.connect()
    con.install_extension("h3", repository="community")
    con.load_extension("h3")

### Unsigned Extensions

To load unsigned extensions, use the `config = {"allow_unsigned_extensions": "true"}` argument to the `duckdb.connect()` method.

## Pages in This Section

##### About this page

- Report content issue
- Edit this page on GitHub