[hypequery](https://hypequery.com/) › ClickHouse to pandas: Governed Metrics in Your Notebooks | hypequery
Index for agents: https://hypequery.com/llms.txt
Canonical page: https://hypequery.com/clickhouse-pandas

> Pull ClickHouse measures into pandas from the same definitions your product uses. Query revenue by month by name instead of copying SQL into a notebook.

[Open sourceApache 2.0](https://github.com/hypequery/hypequery)

ClickHouse pandas

# Get governed ClickHouse metrics into pandas, by name

Notebooks are where numbers drift. hypequery lets analysts query the same measures and dimensions your product and dashboards use, so a DataFrame of “revenue by month” matches what customers see.

[DataFrames guide ](https://hypequery.com/docs/python/dataframes)[Python semantic layer](https://hypequery.com/python-semantic-layer)

Works in

Jupyter, scripts, jobs

Output

pandas, Polars, Arrow

Types

From your definitions

## Every notebook has its own revenue

Analysts copy a SQL query from a dashboard, tweak it, and the number in the deck no longer matches the product.

## Joins and filters get re-derived

Each analysis rediscovers which status counts as completed, which table holds refunds, and how to bucket by week.

## Customer data needs a deliberate scope

A notebook that forgets the tenant filter silently mixes customers. Cross-customer analysis should be explicit.

Notebook

## Query the definitions, not the tables

Import the datasets your application defines, query measures by name with a time grain, and call `to_pandas()`.

- Measures, filters, and relationships come from reviewed code
- Time grains like `by="month"` produce a timezone-aware `period` column
- `to_polars()` and `to_arrow()` work the same way
- The same query runs in a scheduled job without changes

notebook

### Revenue by month into a DataFrame

analysis.ipynb

```
# pip install "hypequery[clickhouse,pandas]==0.1.0b3"
from hypequery.datasets import create_dataset_client, tenant
from hypequery.execution import (
    ClickHouseConnection,
    create_clickhouse_executor,
)

from analytics.datasets import registry  # the definitions your app uses

analytics = create_dataset_client(
    executor=create_clickhouse_executor(ClickHouseConnection.from_env()),
    registry=registry,
)

result = analytics.for_tenant(tenant("org_123")).execute(
    "orders", {"measures": ["revenue", "order_count"], "by": "month"}
)

df = result.to_pandas()  # or result.to_polars(), result.to_arrow()
```

`ClickHouseConnection.from_env()` reads `CLICKHOUSE_URL` or the `CLICKHOUSE_*` variables.

Types and scope

## Typed columns, from the definition

Each column’s type comes from the dataset instead of being guessed from the values. Time buckets become datetimes in the query’s timezone, counts nullable integers, other measures floats. An empty result gets the same dtypes.

Pass `decimals="decimal"` to keep ClickHouse decimals exact. Integers beyond Int64 become unsigned or exact columns rather than overflowing.

Tenant-scoped datasets require a tenant. For internal analysis across every customer, pass `all_tenants()` explicitly so the intent is visible in the code.

notebook

### Typed columns and cross-customer analysis

analysis.ipynb

```
df.dtypes
# period         datetime64[us, UTC]
# revenue                    float64
# order_count                  Int64

#                      period     revenue  order_count
# 0 2025-01-01 00:00:00+00:00  2876716.62         5693
# 1 2025-02-01 00:00:00+00:00  2486385.87         5028

# Keep ClickHouse decimals exact instead of floats:
result.to_pandas(decimals="decimal")

# Internal analysis across every customer has to say so explicitly:
from hypequery.datasets import ExecutionContext, all_tenants

analytics.execute(
    "orders",
    {"measures": ["revenue"], "by": "month"},
    context=ExecutionContext(tenant=all_tenants()),
).to_pandas()
```

Output shown from a sample orders table.

Where teams usually get stuck

## Questions teams ask

### ClickHouse to pandas DataFrame

Query measures through hypequery and call `result.to_pandas()` for typed columns.

### Metrics layer for notebooks

Analysts and the product read the same definitions, so numbers match across decks and dashboards.

### How do I load ClickHouse data into pandas?

With clickhouse-connect, `client.query_df(sql)` returns a pandas DataFrame and `client.insert_df(table, df)` writes one back. For a metric your product already defines, hypequery returns the same DataFrame by measure name instead of SQL.

### clickhouse-connect query_df

clickhouse-connect returns DataFrames for raw SQL. hypequery returns them for named measures, with validation and tenancy.

### Polars or Arrow?

`result.to_polars()` and `result.to_arrow()` use the same column types. Install `hypequery[polars]` or `hypequery[arrow]`.

Examples

## Runnable examples

Every example runs against the same seeded ClickHouse dataset. Scaffold one with `npx @hypequery/cli create <slug> -y`, or browse the [full list](https://hypequery.com/examples).

[Example · Python](https://hypequery.com/examples/streamlit-dashboard)

### [A governed Streamlit dashboard on ClickHouse with pandas export](https://hypequery.com/examples/streamlit-dashboard)

[Build a Streamlit dashboard on ClickHouse that reads only typed hypequery datasets: filters, a time series, breakdowns and a pandas CSV export.](https://hypequery.com/examples/streamlit-dashboard)

[PythonStreamlitPandasClickHouseDashboardsProduct analyticsSemantic layerTime series](https://hypequery.com/examples/streamlit-dashboard)

[Open example](https://hypequery.com/examples/streamlit-dashboard)

Further reading

## Go deeper where it actually helps

### [hypequery vs clickhouse-connect](https://hypequery.com/compare/hypequery-vs-clickhouse-connect)

[When to keep the driver for ad hoc SQL, and when to move shared metrics into datasets.](https://hypequery.com/compare/hypequery-vs-clickhouse-connect)

[Open guide](https://hypequery.com/compare/hypequery-vs-clickhouse-connect)

### [pandas, Polars and Arrow](https://hypequery.com/docs/python/dataframes)

[Column types, timezones, and exact decimals.](https://hypequery.com/docs/python/dataframes)

[Open guide](https://hypequery.com/docs/python/dataframes)

### [Python quick start](https://hypequery.com/docs/quick-start#python)

[Install hypequery and define your first dataset.](https://hypequery.com/docs/quick-start#python)

[Open guide](https://hypequery.com/docs/quick-start#python)

### [Time grains](https://hypequery.com/docs/python/datasets/time-grains)

[Bucket measures by day, week, or month.](https://hypequery.com/docs/python/datasets/time-grains)

[Open guide](https://hypequery.com/docs/python/datasets/time-grains)

### [Caching](https://hypequery.com/docs/python/datasets/caching)

[Reuse results across notebook runs.](https://hypequery.com/docs/python/datasets/caching)

[Open guide](https://hypequery.com/docs/python/datasets/caching)

### [chDB Python tutorial](https://hypequery.com/blog/chdb-python-tutorial)

[Query pandas DataFrames, Parquet and CSV with ClickHouse SQL, in process.](https://hypequery.com/blog/chdb-python-tutorial)

[Open guide](https://hypequery.com/blog/chdb-python-tutorial)

Related pillars

[ClickHouse with TypeScript and Python](https://hypequery.com/clickhouse)[ClickHouse with Python](https://hypequery.com/clickhouse-python)[Python semantic layer](https://hypequery.com/python-semantic-layer)[ClickHouse with SQLAlchemy](https://hypequery.com/clickhouse-sqlalchemy)[ClickHouse time series](https://hypequery.com/clickhouse-time-series)

Next step

## Rebuild one notebook query on a dataset

Pick the query behind your most-shared chart, define it as a dataset, and compare the numbers.

Open source, and it runs in your own backend. Want it hosted?

[Get started ](https://hypequery.com/docs/quick-start#python)[Measures docs](https://hypequery.com/docs/python/datasets/measures)
