pandera icon indicating copy to clipboard operation
pandera copied to clipboard

Support for `typing.Literal` as a type in Column/Series

Open a-recknagel opened this issue 2 years ago • 1 comments

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.

a-recknagel avatar Jan 23 '23 20:01 a-recknagel

Facing this issue at the moment. Would love to have this built-in.

irm-codebase avatar Mar 19 '25 10:03 irm-codebase