2022-04-21
Allow Arbitrary Types (Such as Pandas Dataframe) in Pydantic
- Solution 1 - allow arbitrary types
- Solution 2 - create a pythonic type hint for a pd.Dataframe
- See also
Solution 1 - allow arbitrary types
import pandas as pd
from pydantic import BaseModel
class SubModelInput(BaseModel):
a: pd.DataFrame
b: pd.DataFrame
class Config:
arbitrary_types_allowed = True
Solution 2 - create a pythonic type hint for a pd.Dataframe
import pandas as pd
from pydantic import BaseModel
from typing import TypeVar
PandasDataFrame = TypeVar('pandas.core.frame.DataFrame')
class SubModelInput(BaseModel):
a: PandasDataFrame
b: PandasDataFrame
See also
Pandas DataFrame Validation with Pydantic
Hints from: python - Using Pandas Data Frame as a Type in Pydantic - Stack Overflow
Tags:
pydantic
pandas
type-hints