Skip to main content

A library to transpile Pandas syntax to SQL strings.

Project description

pdql

Lightweight Python library that allows you to write SQL queries using familiar Pandas syntax. It functions as a "lazy compiler," building a syntax tree from your operations and transpiling them into standard SQL strings without executing them or requiring a database connection.

Installation

pip install pdql

Usage

Persistent Dialect & Filtering

from pdql.dataframe import SQLDataFrame
from pdql.dialects import BigQueryDialect

# Initialize with a specific dialect
df = SQLDataFrame("my_table", dialect=BigQueryDialect())

# Filters use dialect-specific quoting (backticks for BigQuery)
query = df[df["age"] > 21]

print(query.to_sql())
# SELECT * FROM `my_table` WHERE (`my_table`.`age` > 21)

Advanced Functions & Method Chaining

Most standard SQL functions are available both as standalone functions and as methods on column objects for clean chaining.

from pdql.dataframe import SQLDataFrame
from pdql import functions as f

df = SQLDataFrame("users")

# Method chaining on columns
query = df[df["email"].lower().starts_with("admin")]

# Complex transformations and types
query = query.groupby("status").agg({
    "salary": "mean",
    "id": "count"
})

print(query.to_sql())
# SELECT "status", AVG("salary") AS "salary_mean", COUNT("id") AS "id_count" 
# FROM "users" 
# WHERE (LOWER("users"."email") LIKE 'admin%') GROUP BY "status"

Common Table Expressions (CTEs)

# Define a subquery
sub = SQLDataFrame("raw_data")[["id", "val"]]
sub = sub[sub["val"] > 10]

# Use it as a source and define the CTE
df = SQLDataFrame("filtered").with_cte("filtered", sub)

print(df.to_sql())
# WITH "filtered" AS (SELECT "id", "val" FROM "raw_data" WHERE ("raw_data"."val" > 10)) 
# SELECT * FROM "filtered"

Development

Use the Makefile for standard tasks:

  • Run Tests: make test
  • Format Code: make format
  • Linting: make lint
  • Build Package: make build

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

pdql-0.2.0.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

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

pdql-0.2.0-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file pdql-0.2.0.tar.gz.

File metadata

  • Download URL: pdql-0.2.0.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pdql-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c7c418e89b03c4ce2522e3ffc49441bce79386ee0d04e29dbfbddd296b7b1040
MD5 0afac215bf96c9ee956da0682fb6221b
BLAKE2b-256 179876e27fe5ab964ea6efff00703dfb72c5617d8a2eea6692943b725692238c

See more details on using hashes here.

File details

Details for the file pdql-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pdql-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 15.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pdql-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d17abd580e059cbb13d413d0ba5023c5a3616bc62316047c9f5f148e0f6c6df5
MD5 ba037b0f1ffbb20da4effa13bc7f6889
BLAKE2b-256 3ed37d6947170638c4b762591b8b84c9102158dc73962143a2b9cd6536df876f

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