Serializing custom checks #417
cosmicBboy
started this conversation in
Show and tell
Replies: 1 comment
-
I agree, this works, but I was hoping to do global checks. I know this is a relatively new part of import pandera
import pandera.extensions as extensions
import pandera.typing as pat
class GlobalSerdes(pandera.SchemaModel):
"""initial schema that includes a global check"""
a: pat.Series[pat.Int64]
b: pat.Series[pat.Int64]
@pandera.dataframe_check()
def _a_gt_b(self, df):
"""ensure that every `a` is greater than every `b`."""
return (df["a"] > df["b"]).all()
class PostSerdes(pandera.SchemaModel):
"""after serdes, it looks like this"""
a: pat.Series[pat.Int64]
b: pat.Series[pat.Int64]
def test_schema_model_serdes():
"""demonstrate that to_yaml strips custom global checks"""
initial = GlobalSerdes.to_schema().to_yaml()
final = PostSerdes.to_schema().to_yaml()
assert initial != final, "global check was stripped"
def test_data_frame_schema():
"""demonstrate that to_yaml fails to serialize registered global checks"""
@extensions.register_check_method(statistics=["a_gt_b"])
def is_a_gt_b(pandas_obj, *, a_gt_b: bool):
return (pandas_obj["a"] > pandas_obj["b"]).all() == a_gt_b
initial = PostSerdes.to_schema().to_yaml()
_ = PostSerdes.to_schema()
_.checks.append(pandera.Check.is_a_gt_b(a_gt_b=True))
final = _.to_yaml()
assert initial != final, "registered global custom check failed"
if __name__ == "__main__":
test_schema_model_serdes()
test_data_frame_schema() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This post answers a question by @antonl, originally posted here: #383 (comment)
With the new extensions API, you can register custom checks into the
Check
namespace, which gives you access as a methodCheck.my_check(...)
or as a keyword argument in the class-based APIcolumn: Series[int] = Field(my_check=...)
Here's an example script:
Output:
Beta Was this translation helpful? Give feedback.
All reactions