Skip to content

pysparky_data_quality.expectations

Explicit dataset and top-level schema expectations for PySpark.

Attributes

NameDescription
CardinalityNo description.
NullPolicyNo description.

Classes

NameDescription
ExpectationDefinitionErrorAn expectation or its DataFrame inputs are invalid or ambiguous.
ExpectationEvaluationErrorPySpark failed while evaluating a dataset or schema expectation.
ExpectationOutcomeImmutable result from one explicit expectation evaluation.

Functions

NameDescription
cardinalityCheck key-to-key cardinality within one DataFrame.
row_countCheck inclusive row-count bounds with one aggregate action.
schema_matchesCompare top-level schema metadata without scanning DataFrame rows.
uniqueCheck uniqueness of one or more key columns with one aggregate action.

Cardinalityattributemodule attribute#

Cardinality = Literal['many_to_one', 'one_to_many', 'one_to_one']

NullPolicyattributemodule attribute#

NullPolicy = Literal['fail', 'equal', 'distinct']

ExpectationDefinitionErrorclass#

class ExpectationDefinitionError(ValueError)

Bases: ValueError

An expectation or its DataFrame inputs are invalid or ambiguous.

ExpectationEvaluationErrorclass#

class ExpectationEvaluationError(RuntimeError)

Bases: RuntimeError

PySpark failed while evaluating a dataset or schema expectation.

ExpectationOutcomeclassdataclass#

class ExpectationOutcome(passed: bool, metrics: tuple[tuple[str, MetricValue], ...] = (), violations: tuple[str, ...] = ())

Immutable result from one explicit expectation evaluation.

Metrics are stored as an ordered tuple of key/value pairs so the outcome remains immutable and straightforward to serialize in a later report.

metricsattributeclass attributeinstance attribute#

metrics: tuple[tuple[str, MetricValue], ...] = ()

passedattributeinstance attribute#

passed: bool

violationsattributeclass attributeinstance attribute#

violations: tuple[str, ...] = ()

cardinalityfunction#

def cardinality(dataframe: DataFrame, source: str | Iterable[str], target: str | Iterable[str], *, relationship: Cardinality = 'many_to_one', null_policy: NullPolicy = 'equal') -> ExpectationOutcome

Check key-to-key cardinality within one DataFrame.

many_to_one requires each source key to map to at most one target key; one_to_many reverses that constraint; one_to_one requires both. Exact duplicate source/target pairs do not violate a relationship. Calling this function is an explicit Spark action boundary.

row_countfunction#

def row_count(dataframe: DataFrame, *, min_rows: int | None = None, max_rows: int | None = None) -> ExpectationOutcome

Check inclusive row-count bounds with one aggregate action.

schema_matchesfunction#

def schema_matches(dataframe: DataFrame, *, required_columns: str | Iterable[str] = (), forbidden_columns: str | Iterable[str] = (), data_types: Mapping[str, str | DataType] | None = None, nullable: Mapping[str, bool] | None = None) -> ExpectationOutcome

Compare top-level schema metadata without scanning DataFrame rows.

String types are compared to the canonical result of PySpark’s DataType.typeName(). Nested type matching is not supported; expected type strings for arrays, maps, structs, and parameterized nested types are rejected.

uniquefunction#

def unique(dataframe: DataFrame, columns: str | Iterable[str], *, null_policy: NullPolicy = 'equal') -> ExpectationOutcome

Check uniqueness of one or more key columns with one aggregate action.

fail rejects any row with a NULL key and also checks duplicate groups; equal treats NULL values as equal when identifying duplicate groups; distinct excludes rows with any NULL key from duplicate detection. Calling this function is an explicit Spark action boundary.