1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Why does Pandera print failing rows with pa.check() and a lambda function but not on...

Discussão em 'Python' iniciado por Stack, Setembro 15, 2025.

  1. Stack

    Stack Membro Participativo

    New to using Pandera. I want it to print the record(s) that fail the check. This is the simple check I want, fail when the system capacity is over 500:

    import pandera.pandas as pa
    import pandas as pd
    schema = pa.DataFrameSchema(
    {
    "TotalSystemCapacityRating": pa.Column(float, pa.Check.lt(500), nullable=False),
    }
    )

    # Run the validation
    print(schema.validate(completed_view, lazy=True))


    It fails correctly, but only tells me the bad values, not details about the records associated with them:


    pandera.errors.SchemaErrors: { "DATA": { "DATAFRAME_CHECK": [ { "schema": null, "column": "TotalSystemCapacityRating", "check": "less_than(500)", "error": "Column 'TotalSystemCapacityRating' failed element-wise validator number 0: less_than(500) failure cases: 6440.0, 6000.0, 9240.0, 22550.0" } ] } }
    ​

    Then I write a more-verbose version of the same check and it shows me the bad records:

    schema = pa.DataFrameSchema(
    checks=[
    pa.Check(
    lambda df: ~(
    (df["TotalSystemCapacityRating"] >= 500)
    ),
    error="Must have TotalSystemCapacityRating < 500.",
    ),
    ],
    )

    print(schema.validate(completed_view, lazy=True))


    It prints the entire dataframe for the four records that fail:


    pandera.errors.SchemaErrors: { "DATA": { "DATAFRAME_CHECK": [ { "schema": null, "column": null, "check": "Must have TotalSystemCapacityRating < 500.", "error": "DataFrameSchema 'None' failed element-wise validator number 0: <Check : Must have TotalSystemCapacityRating < 500.> failure cases: 75e4e529-005d-40d6-a094-2329927ba7e2, 3ed561a9-c4e2-46b7-9973-4e25317f038c, ...
    ​

    That printing is the behavior I want. But the code in the first block is a much simpler way of saying the same thing. Is there a way I can get the printing behavior of the second example with the concise code of the first?

    Continue reading...

Compartilhe esta Página