Skip to content

Commit

Permalink
fix DataFrame Pydantic compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarek-Rolski committed Feb 2, 2025
1 parent 33fe68b commit 29e9609
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions pandera/typing/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
SeriesBase,
)
from pandera.typing.formats import Formats
from pandera.config import config_context

try:
from typing import get_args
Expand Down Expand Up @@ -191,12 +192,28 @@ def _get_schema_model(cls, field):
def __get_pydantic_core_schema__(
cls, _source_type: Any, _handler: GetCoreSchemaHandler
) -> core_schema.CoreSchema:
schema_model = get_args(_source_type)[0]
with config_context(validation_enabled=False):
schema_model = _source_type().__orig_class__.__args__[0]
schema = schema_model.to_schema()
type_map = {

Check warning on line 198 in pandera/typing/pandas.py

View check run for this annotation

Codecov / codecov/patch

pandera/typing/pandas.py#L195-L198

Added lines #L195 - L198 were not covered by tests
"str": core_schema.str_schema(),
"int64": core_schema.int_schema(),
"float64": core_schema.float_schema(),
"bool": core_schema.bool_schema(),
"datetime64[ns]": core_schema.datetime_schema()
}
return core_schema.no_info_plain_validator_function(
functools.partial(
functools.partial(
cls.pydantic_validate,
schema_model=schema_model,
),
json_schema_input_schema=core_schema.list_schema(
core_schema.typed_dict_schema(
{
i:core_schema.typed_dict_field(type_map[str(j.dtype)]) for i,j in schema.columns.items()
},
)
)
)

else:
Expand Down

0 comments on commit 29e9609

Please sign in to comment.