https://github.com/PRQL/pyprql
Python extensions for PRQL
https://github.com/PRQL/pyprql
Last synced: 9 months ago
JSON representation
Python extensions for PRQL
- Host: GitHub
- URL: https://github.com/PRQL/pyprql
- Owner: PRQL
- License: apache-2.0
- Created: 2022-02-03T22:56:55.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2025-04-17T21:01:41.000Z (9 months ago)
- Last Synced: 2025-04-18T11:34:25.472Z (9 months ago)
- Language: Python
- Homepage: https://pyprql.readthedocs.io
- Size: 2.47 MB
- Stars: 99
- Watchers: 6
- Forks: 6
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# pyprql
[](https://github.com/prql/pyprql/actions/workflows/pull-request.yaml)
[](https://pyprql.readthedocs.io/en/latest/?badge=latest)


[](https://github.com/psf/black)
pyprql contains:
- pyprql.pandas_accessor — Pandas integration for PRQL
- pyprql.polars_namespace — Polars integration for PRQL
- pyprql.magic — IPython magic for connecting to databases using `%%prql`
- pyprql.compile — An export of `prqlc`'s `compile` function
For docs, check out the [pyprql docs](https://pyprql.readthedocs.io/), and the
[PRQL Book][prql_docs].
## Installation
```sh
pip install pyprql
```
Or, install with optional dependencies:
```sh
pip install pyprql[polars]
```
## Usage
### Pandas integration
```python
import pandas as pd
import pyprql.pandas_accessor
df = (...)
results_df = df.prql.query("select {age, name, occupation} | filter age > 21")
```
### Polars integration
```python
import polars as pl
import pyprql.polars_namespace
df = (...)
results_df = df.prql.query("select {age, name, occupation} | filter age > 21")
```
### Jupyter Magic
```python
In [1]: %load_ext pyprql.magic
In [2]: %prql postgresql://user:password@localhost:5432/database
In [3]: %%prql
...: from p
...: group categoryID (
...: aggregate {average unitPrice}
...: )
In [4]: %%prql results <<
...: from p
...: aggregate {min unitsInStock, max unitsInStock}
```
### Compilation
This library exposes `prqlc.compile`, so we can simply generate SQL:
```python
import pyprql
pyprql.compile("from artists | select track")
print(pyprql.compile("from artists | select track"))
```
...returns...
```sql
SELECT
track
FROM
artists
```
For context, `prqlc` in Python is the Python binding for the `prqlc` Rust crate, so only
contains functions for compilation; and this library offers broader python
integrations and tooling.
## Support
This project was created by
[@charlie-sanders](https://github.com/charlie-sanders/) &
[@rbpatt2019](https://github.com/rbpatt2019) and is now maintained by the
broader PRQL team.
[prql_docs]: https://prql-lang.org/book