Skip to main content

A Python pivot table DSL — build aggregate queries with a chainable API, generate and execute SQL via DuckDB

Project description

cwind-tableau

A Python pivot table DSL — build aggregate queries with a chainable API, generate and execute SQL.

English | 简体中文


Installation

pip install cwind-tableau

Requires Python >= 3.10.

Quick Start

import cwind_tableau as tab

# Reference columns and apply aggregations
total_sales = tab.col("销量").sum()
avg_sales = tab.col("销量").avg().alias("平均销量")

# Create a view with dimensions and aggregations
view = tab.view(
    table="销量表",
    dims=["产品", "地区"],
    aggs=[total_sales, avg_sales],
)

# Generate SQL
print(view.to_sql())
# SELECT 产品, 地区, SUM(销量) AS "SUM(销量)", AVG(销量) AS "平均销量"
# FROM 销量表
# GROUP BY 产品, 地区

# Execute with DuckDB
result = view.to_query()

Features

Column Reference

tab.col("销量")  # Refer to a column
tab.col("订单日期")  # Column names support Chinese and any UTF-8 characters

Built-in Aggregations

tab.col("销量").sum()  # Sum
tab.col("销量").avg()  # Average
tab.col("销量").count()  # Count
tab.col("销量").count_distinct()  # Distinct count
tab.col("销量").min()  # Minimum
tab.col("销量").max()  # Maximum
tab.col("销量").median()  # Median
tab.col("销量").std()  # Standard deviation
tab.col("x").custom("MY_FUNC")  # Custom aggregation function

Custom Aliases

tab.col("销量").sum().alias("总销量")  # Aggregation alias
tab.col("订单日期").dt.year().alias("年")  # Dimension alias

Date Extraction (.dt accessor)

tab.col("订单日期").dt.year()  # Year
tab.col("订单日期").dt.month()  # Month
tab.col("订单日期").dt.day()  # Day
tab.col("订单日期").dt.quarter()  # Quarter
tab.col("订单日期").dt.week()  # Week number
tab.col("订单日期").dt.month_name()  # Month name (e.g. "January")

Window Dimensions (.fixed())

Turn an aggregation into a window function dimension:

# First order date per customer — used as a dimension, not an aggregation
tab.col("订单日期").min().fixed("客户ID").alias("首单日期")

# Multi-column partition
tab.col("订单日期").min().fixed(["客户ID", "产品"])

Arithmetic Expressions

# Calculate amount = price × quantity, then sum
(tab.col("价格") * tab.col("数量")).sum()

API Reference

tab.col(name: str) -> Column

Create a column reference. Returns a Column object with aggregation and date extraction methods.

tab.view(table: str, dims: list, aggs: list) -> View

Create a pivot view.

Parameter Type Description
table str Source table name
dims list[str | Dimension] Dimension columns for GROUP BY
aggs list[Aggregation] Aggregation expressions for SELECT

View.to_sql() -> str

Generate the SQL string (DuckDB dialect).

View.to_query(con=None) -> duckdb.DuckDBPyResult

Execute the query via DuckDB.

  • Without con: uses duckdb.query() on the default connection
  • With con: uses con.query() on the provided connection

Architecture

cwind-tableau/
├── src/cwind_tableau/
│   ├── api/          # Facade: tab.col, tab.view
│   ├── core/         # Orchestration: View, SQL generation
│   └── models/       # Entities: Column, Aggregation, Dimension
├── tests/
├── docs/
└── examples/

Design Principles

  • Reusable aggregations: Aggregation expressions can be defined as variables and reused across queries
  • Default aliases: Auto-generated aliases (e.g. "SUM(销量)") when .alias() is not used
  • Chainable API: All operations via method chaining — consistent and readable
  • Expressions are objects: Any intermediate result (column, aggregation, dimension) is a Python object that can be stored, passed, and composed

Dependencies

  • DuckDB — SQL execution engine
  • sqlglot — SQL generation and future dialect transpilation

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cwind_tableau-0.1.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cwind_tableau-0.1.0-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file cwind_tableau-0.1.0.tar.gz.

File metadata

  • Download URL: cwind_tableau-0.1.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for cwind_tableau-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fdecff8c18706a4dfa3fb6072ed570f22efa335a1b8c694bf43d2651760c3693
MD5 14544d42c9bf1b03dbcfcd0eb154f9d1
BLAKE2b-256 11297e9ccdc20cb4c1e037a139975a33ea56bca22dd9c6ca5a531cad55ecead8

See more details on using hashes here.

File details

Details for the file cwind_tableau-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cwind_tableau-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for cwind_tableau-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 82da3b6480f159bb83d96f077cf5a81257ac923ff97704d59732e8e50398ce5b
MD5 da535aa234a1a0a99575e2a149076664
BLAKE2b-256 98956fa254a23a50c07178226d4401f1ad9d989c7c0221594766b87b969e2197

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page