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
import polars as pl

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

(
    pl.DataFrame({"a": [0.0, 1.0], "b": [None, 0]})   
    .pipe(DataFrame[Schema]) # <- Add a Schema to your dataframe
    .validate() # Validate it from the Schema annotations!
)
shape: (2, 2)
┌─────┬──────┐
 a    b    
 ---  ---  
 i64  i64  
╞═════╪══════╡
 0    null 
 1    0    
└─────┴──────┘

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"]  # ✅ No issue if "a" exists
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.2.1.tar.gz (68.7 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.2.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for polaroids-0.2.1.tar.gz
Algorithm Hash digest
SHA256 760c3972c0f99a72fd0f50571d34eec7d7c0554ef3e1bbfd1d403ca224f8d320
MD5 f4a3bfeb0da94f5edab1637f7402da2b
BLAKE2b-256 161811ee80d6a36faa6e718fd643c2df75b6d4d952b8f5bcd911a5b32f122110

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for polaroids-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 040d879a87ba1dc1da1f932b96c170e51d2555bb45b1030ff5a7c02292f7083d
MD5 dcf0e0e5d1149f121e933bd1fc542639
BLAKE2b-256 1dfe1c7eb24fb2dbccdf2aa8323b75ba28fe2b0c91d6696da7d3276b5be1029a

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