Replies: 1 comment
-
Looks like you can't do it the way you are wanting to. Dictionaries are Here is an example of how to do it if using a dataframe schema approach: import pandera as pa
import pydantic
from pandera import Column, DataFrameSchema, Check
class MyModel(pydantic.BaseModel):
x: int
y: int
z: int
schema = DataFrameSchema(
{
"foo": Column(pa.Object, Check(lambda x: MyModel(**x))),
},
strict=True,
coerce=True,
)
_ = schema.validate(df) If using a dataframe model, you can use the above with custom checks to derive a similar solution. Unfortunately, this approach provides validation but not strong typing. I suggest pydantic v2 for performance reasons. |
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
-
Hi,
One of my pandas data frame is a list of dictionary. I am trying to figure what is the cleanest way to specify this kind of data within pandera so that it works and mypy is also happy about it. Below is a sample code that currently gives the following error:
TypeError: Cannot interpret 'typing.List[__main__.ReviewDict]' as a data type
Attempt 2:
If I import TypedDict from
typing_extension
then I get the following error:TypeError: TypedDict does not support instance and class checks
.Attempt 3:
if I define
my_dict_column: List[ReviewDict]
then I get the following error:SchemaInitError: Invalid annotation 'my_dict_column: typing.List[__main__.ReviewDict]'
PS: I am using pandera 0.18.0 and pandas 2.2.0
Beta Was this translation helpful? Give feedback.
All reactions