Bring polars data back to Python objects, safely. Validation, schema/query generation.
Project description
Pydantic, for Polars
Type-safe, maintainable interfaces between Polars and Python objects.
uv add pydantic-polars
pydantic_polars.validate
Go from Polars query -> Python objects
Learn the API by example:
from pydantic_polars import validate as plv
# Equivalent to `lf.collect().rows(named=True)`
users = plv.records.collect(lf) # -> list[dict[str, Any]]
# I have a model now. Parse + validate.
users = plv.records[list[UserModel]].collect(lf)
# My polars query always produces exactly 1 user.
user = plv.record[UserModel].collect(lf.limit(1))
# At most, 1 user. But 0 rows might come back. Get User or None.
user = plv.get_record[UserModel].collect(lf.filter(c.name == 'jeff').limit(1))
# NamedTuple instead of dataclass? Also, can we do async?
users = await plv.rows[list[UserNamedTupleRow]].collect_async(lf)
# Need one huge mapping of all name -> age
name_age_map = plv.map[dict[str, int | None]].collect(lf.select(c.name, c.age))
# Everyone's names, please
users_names = plv.column[list[str]].collect(lf.select(c.name))
# Age of oldest person?
oldest_age = plv.item[int | None].collect(lf.select(c.age.max()))
# Can we parallelize those in Rust, on other threads?
users_names, oldest_age = await plv.collect_all_async(
plv.column[list[str]].defer(lf.select(c.name)),
plv.item[int | None].defer(lf.select(c.age.max())),
)
# Only need his age, but 0 rows may come back. Safely get int or None.
age = plv.get_item[int].collect(
lf.filter(c.name == 'jeff').select(c.age).limit(1)
)
1. Pick a Shape
A shape is a fixed, non-configurable representation of a dataframe as plain Python objects.
records means, Produce a list of row dicts. It translates to df.rows(named=True).
records[T] means, Produce T by passing a list of row dicts as input to Pydantic validation.
plv.<shape>.collect(lf) # Returns Default T for <shape>
plv.<shape>[T].collect(lf) # Returns T
- Scalar
item: One value.
- Row-oriented
record: One row as a dict.records: List of many.row: One row as a tuple.rows: List of many.map: The rows of 2 columns, as one {col1: col2} dict.
- Column-oriented
column: One column as a list of values.columns: Tuple of many.column_entry: One (name, column).column_entries: Tuple of many.column_map: Many columns, as one {name: column} dict.
- With table header
table_records: (names, records)table_rows: (names, rows)table_columns: (names, columns)
| Shape | Default T |
Returns | Input query must produce |
|---|---|---|---|
item |
Any |
T |
height == 1, width == 1 |
column |
list[item] |
T |
width == 1 |
row |
tuple[item, ...] |
T |
height == 1 |
record |
dict[name, item] |
T |
height == 1 |
column_entry |
tuple[name, column] |
T |
width == 1 |
map |
dict[item, item] |
T |
width == 2 |
records |
list[record] |
T |
|
rows |
list[row] |
T |
|
columns |
tuple[column, ...] |
T |
|
column_entries |
tuple[column_entry, ...] |
T |
|
column_map |
dict[name, column] |
T |
|
table_columns |
tuple[names, columns] |
T |
|
table_rows |
tuple[names, rows] |
T |
|
table_records |
tuple[names, records] |
T |
|
get_item |
item |
T or None |
height <= 1, width == 1 |
get_row |
row |
T or None |
height <= 1 |
get_record |
record |
T or None |
height <= 1 |
2. Call a method to create T
All shapes have the same methods.
# Single query
result = shape.collect(lf)
result = await shape.collect_async(lf)
result = shape.validate(df) # DataFrame equivalent
# Parallel queries
result1, result2 = plv.collect_all(shape.defer(lf1), shape.defer(lf2))
result1, result2 = await plv.collect_all_async(shape.defer(lf1), shape.defer(lf2))
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_polars-0.0.2.tar.gz.
File metadata
- Download URL: pydantic_polars-0.0.2.tar.gz
- Upload date:
- Size: 35.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
04c29243b00c9ff07dd1c2e36413d271f7878f28169f882118ca095955b80905
|
|
| MD5 |
594c8cc89265c99e7f90bf2286b7cdbc
|
|
| BLAKE2b-256 |
92d31d63452d9b057045e8f57436a01b7f31905391df9591931d9933ab44f6ef
|
File details
Details for the file pydantic_polars-0.0.2-py3-none-any.whl.
File metadata
- Download URL: pydantic_polars-0.0.2-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7676968bbd2e511ba0d0cf46f5e4c429ef5579b53bf8c9e3bbb3665347e2a401
|
|
| MD5 |
265408eb344a0be757fd84625f229a8b
|
|
| BLAKE2b-256 |
11cd5841921fad401981cf8f736ae15b4b8f0ab2252832c157275dfc969f660f
|