Polars Generator¶
Generate Polars DataFrame validators from Flycatcher schemas.
create_polars_validator(schema_cls)
¶
Create a Polars validator from a Schema class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema_cls
|
type
|
A subclass of Schema. |
required |
Returns:
| Type | Description |
|---|---|
PolarsValidator
|
An instance of PolarsValidator for the given schema. |
Source code in src/flycatcher/generators/polars.py
PolarsValidator(schema_cls)
¶
A validator for Polars DataFrames based on schema definition.
This class validates Polars DataFrames against a Flycatcher schema, checking types, constraints, and cross-field validators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema_cls
|
type[Schema]
|
A Flycatcher Schema class to validate against. |
required |
Examples:
>>> from flycatcher import Field, Schema
>>> import polars as pl
>>> class UserSchema(Schema):
... id: int = Field(primary_key=True)
... name: str = Field(min_length=1)
>>> validator = UserSchema.to_polars_validator()
>>> df = pl.DataFrame({"id": [1, 2], "name": ["Alice", "Bob"]})
>>> validated_df = validator.validate(df, strict=True)
Source code in src/flycatcher/generators/polars.py
schema
property
¶
Return the Polars schema dict.
validate(df, strict=True, show_violations=False, fill_nulls=False)
¶
Validate and coerce a DataFrame to match the schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input Polars DataFrame. |
required |
strict
|
bool
|
If True, raise on validation errors. If False, filter invalid rows. |
True
|
show_violations
|
bool
|
If True, show violations in the console. |
False
|
fill_nulls
|
bool
|
If True, replace null values with field defaults (if specified). Note: This is a transformation step. Defaults only apply to missing columns by default. Enable this to also fill existing null values. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Validated DataFrame with correct types. If fill_nulls=True, null values will be replaced with defaults where applicable. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If validation fails and strict=True. |
Notes
Behavior of defaults:
- Missing columns with defaults are always added to the DataFrame
- Existing null values are filled with defaults only if fill_nulls=True
- If a field is nullable without a default, nulls are preserved
Source code in src/flycatcher/generators/polars.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
Usage¶
The create_polars_validator function is typically called via the Schema.to_polars_validator() method:
from flycatcher import Schema, Field
import polars as pl
class UserSchema(Schema):
id: int = Field(primary_key=True)
name: str = Field(min_length=1, max_length=100)
# Generate Polars validator
validator = UserSchema.to_polars_validator()
# Validate a DataFrame
df = pl.DataFrame({
"id": [1, 2, 3],
"name": ["Alice", "Bob", "Charlie"]
})
validated_df = validator.validate(df, strict=True)
Validation Modes¶
The PolarsValidator.validate() method supports different validation modes:
- Strict mode (
strict=True): Raises exceptions on validation errors - Non-strict mode (
strict=False): Filters out invalid rows - Show violations (
show_violations=True): Prints violation details to console - Fill nulls (
fill_nulls=True): Replaces null values with field defaults