Skip to content

Can schema models support an arbitrary number of columns with checks? #991

Answered by cosmicBboy
Midnighter asked this question in Q&A
Discussion options

You must be logged in to vote

hi @Midnighter great question!

You can use the regex column key matching for this. The way it works in the class-based API would be:

import pandas as pd
import pandera as pa
from pandas.api.types import is_integer
from pandera.typing import Series


class VariableColumns(pa.SchemaModel):

    first: Series[str] = pa.Field()
    second: Series[float] = pa.Field(ge=5.0, le=10.0)
    unknown: Series[int] = pa.Field(ge=0, alias="^(first|second)", regex=True)

You can generalize this by specifying the known columns in a private (or global) variable:

import pandas as pd
import pandera as pa
from pandas.api.types import is_integer
from pandera.typing import Series


class VariableColumns(pa.Schem…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Midnighter
Comment options

Answer selected by Midnighter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants