-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
06c1e37
implement pydantic model data type
cosmicBboy 940da66
fix lint
cosmicBboy 9790c97
fixes
cosmicBboy 0f3b7a0
update pydantic type
cosmicBboy ba25769
update linting ci
cosmicBboy 7592dcd
ignore pydantic model in strategies
cosmicBboy c0d783b
add back mypy nox, use np.nan
cosmicBboy ab125db
fix PydanticModel dtype
cosmicBboy 3771c06
fix types
cosmicBboy 37acb98
fix type
cosmicBboy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
"""Unit tests for pydantic datatype.""" | ||
|
||
import pandas as pd | ||
import pytest | ||
from pydantic import BaseModel | ||
|
||
import pandera as pa | ||
from pandera.engines.pandas_engine import PydanticModel | ||
|
||
|
||
class Record(BaseModel): | ||
"""Pydantic record model.""" | ||
|
||
name: str | ||
xcoord: int | ||
ycoord: int | ||
|
||
|
||
class PydanticSchema(pa.SchemaModel): | ||
"""Pandera schema using the pydantic model.""" | ||
|
||
class Config: | ||
"""Config with dataframe-level data type.""" | ||
|
||
dtype = PydanticModel(Record) | ||
coerce = True | ||
|
||
|
||
class PanderaSchema(pa.SchemaModel): | ||
"""Pandera schema that's equivalent to PydanticSchema.""" | ||
|
||
name: pa.typing.Series[str] | ||
xcoord: pa.typing.Series[int] | ||
ycoord: pa.typing.Series[int] | ||
|
||
|
||
def test_pydantic_model(): | ||
"""Test that pydantic model correctly validates data.""" | ||
|
||
@pa.check_types | ||
def func(df: pa.typing.DataFrame[PydanticSchema]): | ||
return df | ||
|
||
valid_df = pd.DataFrame( | ||
{ | ||
"name": ["foo", "bar", "baz"], | ||
"xcoord": [1.0, 2, 3], | ||
"ycoord": [4, 5.0, 6], | ||
} | ||
) | ||
|
||
invalid_df = pd.DataFrame( | ||
{ | ||
"name": ["foo", "bar", "baz"], | ||
"xcoord": [1, 2, "c"], | ||
"ycoord": [4, 5, "d"], | ||
} | ||
) | ||
|
||
validated = func(valid_df) | ||
PanderaSchema.validate(validated) | ||
|
||
expected_failure_cases = pd.DataFrame( | ||
{"index": [2], "failure_case": ["{'xcoord': 'c', 'ycoord': 'd'}"]} | ||
) | ||
|
||
try: | ||
func(invalid_df) | ||
except pa.errors.SchemaError as exc: | ||
pd.testing.assert_frame_equal( | ||
exc.failure_cases, expected_failure_cases | ||
) | ||
|
||
|
||
def test_pydantic_model_init_errors(): | ||
"""SchemaInitError should be raised when coerce=False""" | ||
with pytest.raises(pa.errors.SchemaInitError): | ||
pa.DataFrameSchema(dtype=PydanticModel(Record), coerce=False) | ||
|
||
with pytest.raises(pa.errors.SchemaInitError): | ||
pa.SeriesSchema(dtype=PydanticModel(Record)) | ||
|
||
with pytest.raises(pa.errors.SchemaInitError): | ||
pa.Column(dtype=PydanticModel(Record)) | ||
|
||
with pytest.raises(pa.errors.SchemaInitError): | ||
pa.Index(dtype=PydanticModel(Record)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.