-
-
Notifications
You must be signed in to change notification settings - Fork 344
implement pydantic model data type #779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Codecov Report
@@ Coverage Diff @@
## dev #779 +/- ##
==========================================
- Coverage 97.71% 97.63% -0.08%
==========================================
Files 45 44 -1
Lines 4026 4059 +33
==========================================
+ Hits 3934 3963 +29
- Misses 92 96 +4
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really cool use of DataType !
I have 2 remarks:
-
If coerce is set to False, validation passes since the schema think there are no columns to validate. Pandera should warn if the global dtype is a PydanticModel and coerce is False.
-
PydanticModel(Record).check(df)
always return False. It's not a big deal since most users won't call it. We can overridecheck
and raise a NotImplementedError for now.
To expand on 2., one problem with the current DataType.check()
method is that it validates the data type without access to the data itself. Therefore you cannot re-use the coerce
logic to validate with check
. I've ran into this problem when implementing decimal and date dtypes, which is why I've been holding back my PR. Those dtypes can be coerced by not validated without looking at the data. I will open a separate issue because it deserves its own discussion.
Minimal example:
import pandas as pd
from pydantic import BaseModel
import pandera as pa
from pandera.engines.pandas_engine import PydanticModel
class Record(BaseModel):
"""Pydantic record model."""
age: int
class PydanticSchema(pa.SchemaModel):
"""Pandera schema using the pydantic model."""
class Config:
"""Config with dataframe-level data type."""
dtype = PydanticModel(Record)
coerce = False # <------
df = pd.DataFrame({"age": [21, "foo"]})
PydanticSchema.validate(df) # passes
#> age
0 21
1 foo
PydanticModel(Record).check(df) # unsupported, expects a data type
#> False
Did you see my 2 remarks above?
^ I think this one is important to fix before merging. |
Ah, right, those slipped my mind. I think for (1) I'm gonna play around with adding a |
@jeffzi I decided to special-case the PydanticModel dtype in the DataFrameSchema constructor, raising a SchemaInitError if coerce=False and dtype is a PydanticModel... in this case I thought it best to have pandera barf in this case instead of a warning. |
* add imports to fastapi docs * Add option to disallow duplicate column names (#758) * ENH: add duplicate detection to dataframeschema * ENH: propagate duplicate colnames check to schemamodel * Add getter setter property * make schemamodel actually work, update __str__ * fix __repr__ as well * fix incorrect default value * black formatting has changed * invert parameter naming convention * address other PR comments * fix doctests, comma in __str__ * maybe fix sphinx errors * fix ci and mypy tests * Update test_schemas.py * fix lint Co-authored-by: cosmicBboy <[email protected]> * Make SchemaModel use class name, define own config (#761) * Make SchemaModel use class name, define own config * fix * fix * fix * fix tests * fix lint and docs * add test Co-authored-by: cosmicBboy <[email protected]> * implement coercion-on-initialization for DataFrame[SchemaModel] types (#772) * implement coercion-on-initialization * pylint * Update tests/core/test_model.py Co-authored-by: Matt Richards <[email protected]> Co-authored-by: Matt Richards <[email protected]> * update conda install instructions (#776) * add documentation for pandas_engine.DateTime (#780) * add documentation for pandas_engine.DateTime * fix removed numpy_engine.Object doc * set default n_failure_cases to None (#784) * Update filtering columns for performance reasons. (#777) * Update filtering columns for performance reasons. * Update pandera/schemas.py * Update schemas.py * Update schemas.py * Bugfix in schemas.py Co-authored-by: Niels Bantilan <[email protected]> * implement pydantic model data type (#779) * make finding coerce failure cases faster (#792) * make finding coerce failure cases faster * fix tests * remove unneeded import * fix tests, coverage * update docs for 0.10.0 (#795) * add pyspark support, deprecate koalas (#793) * add support for pyspark.pandas, deprecate koalas * update docs * add type check in pandas generics * update docs * clean up ci * fix mypy, generics * fix generic hack * improve coverage * Add overloads to `schema.to_yaml` (#790) * Add overloads to `to_yaml` * Update schemas.py Co-authored-by: Niels Bantilan <[email protected]> * add support for logical data types * add initial support for decimal * fix dtype check * Feature: Add support for Generic to SchemaModel (#810) * Adapt SchemaModel so that it can inherit from typing.Generic * Extend SchemaModel to enable generic types in fields * fix linter Co-authored-by: Thomas Willems <[email protected]> Co-authored-by: cosmicBboy <[email protected]> * fix pandas_engine.DateTime.coerce_value not consistent with coerce (#827) * pyspark docs fixes * fix koalas link to pyspark * bump version 0.10.1 * fix pandas_engine.DateTime.coerce_value not consistent with coerce Co-authored-by: cosmicBboy <[email protected]> * Refactor logical type check method * add logical types tests * add back conftest * fix test_invalid_annotations * fix ray initialization in setup_modin_engine * fix logical type validation when output is an iterable * add Decimal data type to pandera.__init__ * remove DataType.is_logical * add logical types documentation * Update dtypes.rst * Update dtypes.rst * increase coverage * fix SchemaErrors.failure_cases with logical types * fix modin compatibility for logical type validation * fix prepare_series_check_output compatibility with pyspark * fix mypy error * Update dtypes.rst Co-authored-by: cosmicBboy <[email protected]> Co-authored-by: Matt Richards <[email protected]> Co-authored-by: Sean Mackesey <[email protected]> Co-authored-by: Ferdinand Hahmann <[email protected]> Co-authored-by: Robert Craigie <[email protected]> Co-authored-by: tfwillems <[email protected]> Co-authored-by: Thomas Willems <[email protected]>
* add imports to fastapi docs * Add option to disallow duplicate column names (#758) * ENH: add duplicate detection to dataframeschema * ENH: propagate duplicate colnames check to schemamodel * Add getter setter property * make schemamodel actually work, update __str__ * fix __repr__ as well * fix incorrect default value * black formatting has changed * invert parameter naming convention * address other PR comments * fix doctests, comma in __str__ * maybe fix sphinx errors * fix ci and mypy tests * Update test_schemas.py * fix lint Co-authored-by: cosmicBboy <[email protected]> * Make SchemaModel use class name, define own config (#761) * Make SchemaModel use class name, define own config * fix * fix * fix * fix tests * fix lint and docs * add test Co-authored-by: cosmicBboy <[email protected]> * implement coercion-on-initialization for DataFrame[SchemaModel] types (#772) * implement coercion-on-initialization * pylint * Update tests/core/test_model.py Co-authored-by: Matt Richards <[email protected]> Co-authored-by: Matt Richards <[email protected]> * update conda install instructions (#776) * add documentation for pandas_engine.DateTime (#780) * add documentation for pandas_engine.DateTime * fix removed numpy_engine.Object doc * set default n_failure_cases to None (#784) * Update filtering columns for performance reasons. (#777) * Update filtering columns for performance reasons. * Update pandera/schemas.py * Update schemas.py * Update schemas.py * Bugfix in schemas.py Co-authored-by: Niels Bantilan <[email protected]> * implement pydantic model data type (#779) * make finding coerce failure cases faster (#792) * make finding coerce failure cases faster * fix tests * remove unneeded import * fix tests, coverage * update docs for 0.10.0 (#795) * add pyspark support, deprecate koalas (#793) * add support for pyspark.pandas, deprecate koalas * update docs * add type check in pandas generics * update docs * clean up ci * fix mypy, generics * fix generic hack * improve coverage * Add overloads to `schema.to_yaml` (#790) * Add overloads to `to_yaml` * Update schemas.py Co-authored-by: Niels Bantilan <[email protected]> * add support for logical data types * add initial support for decimal * fix dtype check * Feature: Add support for Generic to SchemaModel (#810) * Adapt SchemaModel so that it can inherit from typing.Generic * Extend SchemaModel to enable generic types in fields * fix linter Co-authored-by: Thomas Willems <[email protected]> Co-authored-by: cosmicBboy <[email protected]> * fix pandas_engine.DateTime.coerce_value not consistent with coerce (#827) * pyspark docs fixes * fix koalas link to pyspark * bump version 0.10.1 * fix pandas_engine.DateTime.coerce_value not consistent with coerce Co-authored-by: cosmicBboy <[email protected]> * Refactor logical type check method * add logical types tests * add back conftest * fix test_invalid_annotations * fix ray initialization in setup_modin_engine * fix logical type validation when output is an iterable * add Decimal data type to pandera.__init__ * remove DataType.is_logical * add logical types documentation * Update dtypes.rst * Update dtypes.rst * increase coverage * fix SchemaErrors.failure_cases with logical types * fix modin compatibility for logical type validation * fix prepare_series_check_output compatibility with pyspark * fix mypy error * Update dtypes.rst Co-authored-by: cosmicBboy <[email protected]> Co-authored-by: Matt Richards <[email protected]> Co-authored-by: Sean Mackesey <[email protected]> Co-authored-by: Ferdinand Hahmann <[email protected]> Co-authored-by: Robert Craigie <[email protected]> Co-authored-by: tfwillems <[email protected]> Co-authored-by: Thomas Willems <[email protected]>
fixes #764