pandera.api.dataframe.model_components.ParsedField¶

pandera.api.dataframe.model_components.ParsedField(*, parser, source=None, on_error='raise', **field_kwargs)[source]¶

Declare a column derived from other columns of the same dataframe.

ParsedField is Field() plus the provenance of the column’s values, so a model states where a column comes from instead of leaving it to a schema-level parsers list:

class Tickets(pa.DataFrameModel):
    body: str
    n_words: int = pa.ParsedField(
        source="body",
        parser=lambda s: s.str.split().str.len(),
        ge=1,
    )
Parameters:
  • parser (Any) – a callable producing the column’s values, or an object implementing the ColumnParser protocol. A callable receives the source column as a Series when a single source is declared, and the selected source columns as a DataFrame otherwise.

  • source (Union[str, Iterable[str], None]) – the column(s) this column is derived from. Defaults to the model’s Config.parser_source.

  • on_error (str) – what to do when the parser fails.

  • field_kwargs (Any) – everything Field() accepts.

Return type:

Any