Skip to main content

Inserting data with ClickHouse Connect: Advanced usage

InsertContexts

ClickHouse Connect executes Native-format inserts, the insert and insert_df methods, within an InsertContext. The insert_arrow, insert_df_arrow, and raw_insert methods send their payloads directly and don’t use one. The InsertContext includes all the values sent as arguments to the client insert method. In addition, when an InsertContext is originally constructed, ClickHouse Connect retrieves the data types for the insert columns required for efficient Native format inserts. By reusing the InsertContext for multiple inserts, this “pre-query” is avoided and inserts are executed more quickly and efficiently. An InsertContext can be acquired using the client create_insert_context method. The method takes the same arguments as the insert function, except for context itself. Note that only the data property of InsertContexts should be modified for reuse. This is consistent with its intended purpose of providing a reusable object for repeated inserts of new data to the same table.
InsertContexts include mutable state that is updated during the insert process, so they’re not thread safe.

Write formats

Write formats are implemented for a limited number of types. In most cases ClickHouse Connect automatically determines the correct write format for a column from its first non-null data value. For example, when the first value for a DateTime column is an integer, the client treats it as an epoch second. It is normally unnecessary to override a write format, but the methods in clickhouse_connect.datatypes.format can set one globally. Container wrappers such as Array, Nullable, and LowCardinality preserve the element type’s formatting behavior.

Write format options

Specialized insert methods

ClickHouse Connect provides specialized insert methods for common data formats:
  • insert_df — Insert a Pandas DataFrame as column-oriented Native data. It also supports explicit column names/types or a reusable InsertContext.
  • insert_arrow — Insert a PyArrow Table using the ClickHouse Arrow input format.
  • insert_df_arrow — Insert an Arrow-backed Pandas DataFrame or a Polars DataFrame. Pandas columns must all use Arrow-backed dtypes.
All three methods accept database, settings, and per-request HTTP transport_settings.
A NumPy array is a valid Sequence of Sequences and can be used as the data argument to the main insert method, so a specialized method isn’t required.

Pandas DataFrame insert

PyArrow Table insert

Arrow-backed DataFrame insert (pandas 2.x)

Create a table from a PyArrow schema

create_table_from_arrow_schema builds a CREATE TABLE statement from common scalar Arrow fields. The mapping covers signed and unsigned integers, floating-point values, booleans, strings, dates, and timestamps. It intentionally creates non-nullable ClickHouse columns and raises TypeError for unsupported Arrow types, so review the generated DDL before executing it.

Time zones

When inserting Python datetime objects into DateTime or DateTime64 columns, ClickHouse Connect converts them to epoch values.

Timezone-aware datetime objects

Timezone-aware objects preserve the represented instant. The source timezone does not need to match the timezone declared on the ClickHouse column.
ClickHouse Connect uses the standard library zoneinfo module. The driver no longer depends on pytz.

Timezone-naive datetime objects

Python interprets a naive datetime in the system’s local timezone when .timestamp() is called. That makes inserts dependent on the environment. Prefer one of these approaches:
  1. Use timezone-aware datetime objects.
  2. Ensure the process timezone is UTC.
  3. Attach the intended timezone or convert to an epoch integer explicitly.

DateTime columns with timezone metadata

ClickHouse columns can declare timezone metadata, for example DateTime('America/Denver') or DateTime64(3, 'Asia/Tokyo'). The metadata controls how values are presented when queried. When inserting into such a column, ClickHouse Connect converts the Python value according to its own tzinfo. When queried, the result uses the column timezone unless a per-column override is supplied with the column_tzs argument. The query_tz argument doesn’t override a column’s declared timezone.

File inserts

clickhouse_connect.driver.tools.insert_file streams a local file into an existing table and delegates parsing to ClickHouse. Input-format settings such as input_format_allow_errors_ratio and input_format_allow_errors_num can be passed through settings.
For an AsyncClient, await insert_file_async with the same arguments:
The async helper reads the file in a worker thread before awaiting raw_insert, so the file contents are held in memory.
Last modified on July 23, 2026