Skip to main content

Validate you Generic polars dataframes

Project description

Polars on steroids!

This package provides a generic extension to Polars DataFrame, allowing data validation and typing goodies.

Features

  • Generic DataFrame: Ensures type safety using Python's TypedDict.
  • Data Validation: Checks that the DataFrame conforms to the expected schema.
  • Custom Checks: Leverage the power of polars expression to add custom checks.
  • Lightweight: No dependencies (except polars)!

Installation

pip install polaroids

Documentation

๐Ÿ“– Read the full documentation here: Project Documentation

Basic Usage

from typing import Annotated, TypedDict
from polaroids import DataFrame, Field
from polaroids.types import int8
import polars as pl

class SubSchema(TypedDict):
    c: list[bool]
    d: str

class Schema(TypedDict):
    a: Annotated[int8, Field(
        sorted="ascending",
        coerce=True,
        unique=True,
        checks=[lambda d: d.ge(0)],
    )]
    b: int | None
    s: SubSchema

df = (
    pl.DataFrame({
        "a": [0.0, 1.0], 
        "b": [None, 0], 
        "s": [{"c": [True], "d": "0"}, {"c": [True, False], "d": "1"}]
    })   
    .pipe(DataFrame[Schema]) # <- Add a Schema to your dataframe
    .validate() # Validate it from the Schema annotations!
)
df
shape: (2, 3)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ a   โ”† b    โ”† s                   โ”‚
โ”‚ --- โ”† ---  โ”† ---                 โ”‚
โ”‚ i8  โ”† i64  โ”† struct[2]           โ”‚
โ•žโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ก
โ”‚ 0   โ”† null โ”† {[true],"0"}        โ”‚
โ”‚ 1   โ”† 0    โ”† {[true, false],"1"} โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Typing Benefits with polaroids

One of the key advantages of polaroids is its strong typing support. You can use classic Polars functions while benefiting from improved type checking and autocompletion in your IDE, reducing runtime errors.

row = df.row(0, named=True)
row["a"]  # โœ… Type checker agree; resulting type is `int`
row["s"]["c"][0]  # โœ… Type checker is happy; resulting type is `bool`
row["not_exists"] # โŒ Type error detected immediately!

Comparison with Alternatives

Compared to Pandera and Patito, polaroids' typing system is based on TypedDict rather than Pydantic's BaseModel.

Pydantic is a great tool, but when validating large Polars DataFrames, it's preferable to use Polars expressions for efficiency. Given this, a dependency on Pydantic is not particularly relevant.

Moreover, to benefit from typing with Pandera or Patito, you need to instantiate Pydantic objects, which introduces a runtime penalty, especially when iterating over rows.

In contrast, polaroids relies on stub-based typing, meaning there is no runtime penalty. As a result, polaroids is extremely lightweight, with no dependencies (neither Pandas nor Pydantic).

Contribution

We welcome contributions to polaroids! Follow these steps to set up your development environment and ensure your changes meet project standards.

1. Clone the Repository

git clone git@github.com:gab23r/polaroids.git
cd polaroids

2. Set Up the Environment

uv sync

3. Pre-commit Hooks

uv run pre-commit install

To manually run checks before committing:

uv run pre-commit run --all-files

4. Running Tests

uv run pytest tests

Thanks and happy coding! ๐Ÿš€

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

polaroids-0.3.0.tar.gz (68.4 kB view details)

Uploaded Source

Built Distribution

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

polaroids-0.3.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file polaroids-0.3.0.tar.gz.

File metadata

  • Download URL: polaroids-0.3.0.tar.gz
  • Upload date:
  • Size: 68.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.28

File hashes

Hashes for polaroids-0.3.0.tar.gz
Algorithm Hash digest
SHA256 f2df1c843efceaf81483f8e70ac83c69c1b0a6c166cf383d0678fc95c699c11d
MD5 f7f329d171861d8611ae1cecccec2c5c
BLAKE2b-256 0493772db590fe37463eeb1d366b8b41ddfbc659d7fe8b16f3395d444e744e0e

See more details on using hashes here.

File details

Details for the file polaroids-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: polaroids-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.28

File hashes

Hashes for polaroids-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3136d5b5d7dd394ddc899350660fdc1707709ad8469f11448cf79ea5a5a9cce8
MD5 6bfe58f78089d1edd62c3ce40a0bb030
BLAKE2b-256 76d55bc4d5806790ac3ee173c1a6203dd49db00f75c9475bd6ccf64b126c0743

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