Validate you Generic polars dataframes
Project description
DataFrame Extension for Polars
This package provides a generic extension to Polars DataFrame, allowing schema validation and custom validation logic through class-based definitions.
Features
- Generic DataFrame: Ensures type safety using Python's
TypedDict. - Schema Validation: Automatically checks that the DataFrame conforms to the expected schema.
- Custom Validation Hooks: Define additional validation methods prefixed with
check_. - Improved Typing for Rows: Provides better type safety for
rows(named=True).
Installation
This package depends on polars. Install it with:
pip install polaric
Usage
Defining a Schema
Schemas are defined using Python's TypedDict:
from typing import TypedDict
from polaric import DataFrame
import polars as pl
class BasicSchema(TypedDict):
a: int
df = pl.DataFrame({"a": [0, 1]})
basic_df = DataFrame[BasicSchema](df)
basic_df.validate() # Ensures schema correctness
Adding Custom Validations
Extend DataFrame and define validation methods prefixed with check_:
class BasicSchemaDataFrame(DataFrame[BasicSchema]):
def check_a_is_positive(self) -> Self:
assert self.select(pl.col("a").ge(0).all()).item(), "Column a contains negative values!"
return self
# Example usage
df = pl.DataFrame({"a": [0, 1]})
basic_df = BasicSchemaDataFrame(df)
basic_df.validate() # Passes validation
# This will raise an AssertionError
df_invalid = pl.DataFrame({"a": [-1, 1]})
BasicSchemaDataFrame(df_invalid).validate()
Get typing goodies !
Ensure row retrieval maintains proper types:
row = basic_df.rows(named=True)[0]
# row is a typedDict !
Project details
Release history Release notifications | RSS feed
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 polaric-0.1.0.tar.gz.
File metadata
- Download URL: polaric-0.1.0.tar.gz
- Upload date:
- Size: 24.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.28
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b0e97adc2c1ecc513433872785b58c259966d5149ec89c99f0e06f856e2b43d3
|
|
| MD5 |
f6e4fcff29f9b069eadf30e0dc1f2d2c
|
|
| BLAKE2b-256 |
6ab65e7494c86b288c36e671c8f1c7a7fbfc1f865a6d0f35875e644fa50c58fb
|
File details
Details for the file polaric-0.1.0-py3-none-any.whl.
File metadata
- Download URL: polaric-0.1.0-py3-none-any.whl
- Upload date:
- Size: 2.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.28
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fab4b5f2a173da757168de74ebf356137184c920c7aff4f07604c2170a4a19fb
|
|
| MD5 |
a220b952af709bb5ae0ffbdd234813db
|
|
| BLAKE2b-256 |
2cfac40575fa3bf13114acc7441e3c5e253c987da5f7e4e619bd9fdf52fad815
|