Columns¶
What one column declares, and the pieces that make up a declaration.
ColSpec¶
ColSpec
dataclass
¶
ColSpec(dtype: DataType | type[DataType], col_name: str | None = None, seed_name: str | None = None, nullable: bool = False, bounds: Bound[Any] | tuple[Any, Any] | list[Any] | None = None, tags: str | Sequence[str] | None = (), unique: bool = False, null_probability: float = _DEFAULT_NULL_PROBABILITY, string_length: Bound[int] | tuple[int, int] | list[int] | None = None, list_length: Bound[int] | tuple[int, int] | list[int] | None = None, fields: Mapping[str, ColSpec] | None = None, format: str | None = None, pattern: str | None = None, distribution: str | None = None, distribution_params: dict[str, float] | None = None, choices: Sequence[Any] | dict[Any, float] | None = None, weights: Sequence[float] | None = None, rules: Sequence[ColRule] = (), validators: Check | Expr | Pred | Sequence[Check | Expr | Pred] | None = ())
One column's declaration: its type, and every claim made about its values.
A ColSpec is what generate() samples from and what validate() checks
against, so each field below is a claim both sides read.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dtype
|
DataType | type[DataType]
|
The data type of the column. |
required |
col_name
|
str | None
|
Overrides the column's name in the generated/validated DataFrame.
Declaring columns as class attributes on a |
None
|
seed_name
|
str | None
|
The name the column's seed is derived from, when it is not the
column's own. Generation seeds each column from the frame seed and
the column name, so renaming a column changes the values it
produces; a column declared with |
None
|
nullable
|
bool
|
Whether the column allows null values. |
False
|
bounds
|
Bound | tuple | list | None
|
The inclusive range of values allowed in the column, as a An open end means different things to the two consumers of this field,
deliberately. |
None
|
tags
|
str | Sequence[str]
|
Tag or tags classifying the column, for later selection. |
()
|
unique
|
bool
|
Whether values in the column must be distinct. Generation draws the
column without replacement; nulls are exempt. Cannot be combined with
|
False
|
null_probability
|
float
|
Probability of a value being null. Must be between 0 and 1, and only
has effect alongside |
_DEFAULT_NULL_PROBABILITY
|
string_length
|
Bound | tuple[int, int] | list[int] | None
|
The inclusive range of string lengths, where that applies. |
None
|
list_length
|
Bound | tuple[int, int] | list[int] | None
|
For a |
None
|
fields
|
Mapping[str, ColSpec] | None
|
For a A field is a value, not a column: A |
None
|
format
|
str | None
|
The shape a |
None
|
pattern
|
str | None
|
A regular expression every value of a |
None
|
distribution
|
str | None
|
The name of the probability distribution for the column's values
(e.g. |
None
|
distribution_params
|
dict[str, float] | None
|
Parameters specific to the chosen distribution. |
None
|
choices
|
tuple | list | dict | None
|
A finite set of allowed values. A dict maps each choice to its weight. |
None
|
weights
|
tuple[float, ...] | list[float] | None
|
Weights associated with |
None
|
rules
|
tuple[ColRule, ...]
|
Rules ( |
()
|
validators
|
Check | Expr | Pred | Sequence[...] | None
|
A single-column business rule, or several: each either a |
()
|
Examples:
>>> ColSpec(pl.Int64, bounds=(1, 100), nullable=True, null_probability=0.1)
>>> ColSpec(pl.String, choices=["NEW", "PAID"], weights=[3.0, 1.0])
value_dtype
property
¶
The dtype each value has: the element's for a List or Array
column, the column's own otherwise. Every field that describes a
value is checked against this.
Bound¶
Bound
dataclass
¶
An inclusive [min, max] range, used for numeric bounds, temporal ranges, and string lengths.
Either endpoint may be None, meaning that side is unconstrained -- see
ColSpec.bounds, the only field that accepts an open end.
ColRule¶
ColRule
dataclass
¶
ColRule(when: Pred, choices: tuple, weights: tuple[float, ...] | None = None)
Restricts a column's generated values on rows where when matches.
Applied as a pass over the generated frame: rows where when matches get
a value resampled uniformly (or according to weights) from choices
instead of whatever was freely generated for them. Multiple rules on the
same column are checked in declaration order, first match wins (like
SQL CASE/WHEN).
when is evaluated against the frame as it stands when the rule runs,
and the passes run in dependency order: a rule keyed on a column that
another rule or a foreign key rewrites sees the rewritten values -- the
same values validation checks the rule against. Two columns whose rules
each read what the other writes have no such order and are rejected at
declaration.
when is a predicate built with polspec.col(), not an arbitrary polars
expression, so that every rule can round-trip through a spec file:
ColRule(when=col("region") == "UK", choices=["RoyalMail"])
ColRule(when=col("region").is_in(["US", "EU"]) & (col("qty") > 10), choices=["UPS"])
Check¶
Check
dataclass
¶
Check(expr: Expr | Pred, name: str | None = None, description: str | None = None, ignore_nulls: bool = True, pred: Pred | None = None)
A declarative multi-column validation constraint evaluated as a Polars boolean expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr | Pred
|
The boolean condition each row must satisfy: a Polars expression, or a
predicate built with |
required |
name
|
str | None
|
A human-readable identifier for the check constraint (e.g. 'total_gte_subtotal'). If omitted, defaults to the string representation of the expression. |
None
|
description
|
str | None
|
An optional description detailing the business logic or rationale for this check. |
None
|
ignore_nulls
|
bool
|
Whether rows evaluating to null in the check condition are considered valid (standard SQL CHECK constraint semantics). If False, null results are treated as failures. |
True
|
Examples: