Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Enum #1798

Merged
merged 4 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions pandera/engines/polars_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,11 +680,49 @@

@classmethod
def from_parametrized_dtype(cls, polars_dtype: pl.Categorical):
"""Convert a :class:`polars.Decimal` to
a Pandera :class:`pandera.engines.polars_engine.Decimal`."""
"""Convert a :class:`polars.Categorical` to
a Pandera :class:`pandera.engines.polars_engine.Categorical`."""
return cls(ordering=polars_dtype.ordering)


@Engine.register_dtype(equivalents=[pl.Enum])
@immutable(init=True)
class Enum(DataType):
"""Polars enum data type."""

type = pl.Enum

categories: pl.Series

def __init__( # pylint:disable=super-init-not-called
self,
categories: Union[pl.Series, Iterable[str], None] = None,
) -> None:
object.__setattr__(self, "categories", categories)
object.__setattr__(self, "type", pl.Enum(categories=categories))

@classmethod
def from_parametrized_dtype(cls, polars_dtype: pl.Enum):
"""Convert a :class:`polars.Enum` to
a Pandera :class:`pandera.engines.polars_engine.Enum`."""
return cls(categories=polars_dtype.categories)

def check(
self,
pandera_dtype: dtypes.DataType,
data_container: Optional[PolarsDataContainer] = None,
) -> Union[bool, Iterable[bool]]:
try:
pandera_dtype = Engine.dtype(pandera_dtype)
except TypeError:
return False

Check warning on line 718 in pandera/engines/polars_engine.py

View check run for this annotation

Codecov / codecov/patch

pandera/engines/polars_engine.py#L715-L718

Added lines #L715 - L718 were not covered by tests

return (

Check warning on line 720 in pandera/engines/polars_engine.py

View check run for this annotation

Codecov / codecov/patch

pandera/engines/polars_engine.py#L720

Added line #L720 was not covered by tests
self.type == pandera_dtype.type
and (self.type.categories == pandera_dtype.categories).all() # type: ignore
)


@Engine.register_dtype(
equivalents=["category", dtypes.Category, dtypes.Category()]
)
Expand Down
6 changes: 6 additions & 0 deletions tests/polars/test_polars_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,12 @@ def test_polars_struct_nested_type(inner_dtype_cls):
pl.List(pl.Object()),
pl.LazyFrame({"0": [[1.0, 2.0, 3.0]]}),
],
# Enum
[
pl.Enum(categories=["yes", "no"]),
pl.Enum(categories=["yes", "no", "?"]),
pl.LazyFrame({"0": ["yes", "yes", "no"]}),
],
# Struct
[
pl.Struct({"a": pl.Utf8(), "b": pl.Int64(), "c": pl.Float64()}),
Expand Down