Skip to main content

Validate you Generic polars dataframes

Project description

Polars Generic

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

pip install polaroids

Usage

Defining a Schema

Schemas are defined using Python's TypedDict:

from typing import TypedDict
from polaroids import DataFrame, Field
import polars as pl

class BasicSchema(TypedDict):
    a: Annotated[pl.Int64, Field(
        sorted="ascending",
        coerce=True,
        unique=True,
        checks=[lambda d: d.ge(0)],
    )]
    b: str

df = pl.DataFrame({"a": [0, 1], "b": ["a", "b"]})
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


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.1.0.tar.gz (33.8 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.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for polaroids-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9e45ec9c04b2bb56872d6ed563d16b4e828457e71f0c7e3f3803a2302091f0b8
MD5 4860c0017855b82651c60313fa15cccc
BLAKE2b-256 5bd51bb67c53669233cfafb98cd5c3cdaea925e1e4af82f44f60ee07fb518826

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for polaroids-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8f88005453dafa1985badaacf00dfe04603b38b619db5909e1225af43e1c4d19
MD5 d7708ca1e88a09ab6330ee10e2912a8c
BLAKE2b-256 2037ff0009e669d67580753be81035751f8a579bcef17c5d8a607b0ea678c9b3

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