pandera
pandera copied to clipboard
Support for `typing.Literal` as a type in Column/Series
I like to use Literal whenever possible, which is not supported right now:
import pandera as pa
from pandera.typing import Series
from typing import Literal
class Schema(pa.SchemaModel):
a: Series[Literal[1, 2, 3]]
Schema.to_schema() # TypeError: Cannot interpret '1' as a data type
What I can do instead is to use Series[int] = pa.Field(isin=[1, 2, 3]) or Series[Annotated[pa.Category, [1, 2, 3], True]], with the latter one requiring coerce=True in order to be usable ad-hoc.
I think it would be nice if Literal would work out of the box, for example by simply transforming it into one of the current alternatives under the hood.
Facing this issue at the moment. Would love to have this built-in.