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

Add a generic Series type for polars #1595

Merged
merged 2 commits into from
Apr 26, 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
14 changes: 3 additions & 11 deletions pandera/api/polars/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pandera.engines import polars_engine as pe
from pandera.errors import SchemaInitError
from pandera.typing import AnnotationInfo
from pandera.typing.polars import Series

Check warning on line 24 in pandera/api/polars/model.py

View check run for this annotation

Codecov / codecov/patch

pandera/api/polars/model.py#L24

Added line #L24 was not covered by tests


class DataFrameModel(_DataFrameModel[pl.LazyFrame, DataFrameSchema]):
Expand Down Expand Up @@ -74,6 +75,7 @@
if (
annotation.origin is None
or isinstance(annotation.origin, pl.datatypes.DataTypeClass)
or annotation.origin is Series
or engine_dtype
):
if check_name is False:
Expand All @@ -94,19 +96,9 @@
columns[field_name] = Column(**column_kwargs)

else:
origin_name = (
f"{annotation.origin.__module__}."
f"{annotation.origin.__name__}"
)
msg = (
" Series[TYPE] annotations are not supported for polars. "
"Use the bare TYPE directly"
if origin_name == "pandera.typing.pandas.Series"
else ""
)
raise SchemaInitError(
f"Invalid annotation '{field_name}: "
f"{annotation.raw_annotation}'.{msg}"
f"{annotation.raw_annotation}'."
)

return columns
Expand Down
14 changes: 13 additions & 1 deletion pandera/typing/polars.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

from packaging import version

from pandera.typing.common import DataFrameBase, DataFrameModel
from pandera.typing.common import (

Check warning on line 7 in pandera/typing/polars.py

View check run for this annotation

Codecov / codecov/patch

pandera/typing/polars.py#L7

Added line #L7 was not covered by tests
DataFrameBase,
DataFrameModel,
SeriesBase,
)


try:
Expand Down Expand Up @@ -35,3 +39,11 @@

*new in 0.19.0*
"""

# pylint: disable=too-few-public-methods
class Series(SeriesBase, pl.Series, Generic[T]):

Check warning on line 44 in pandera/typing/polars.py

View check run for this annotation

Codecov / codecov/patch

pandera/typing/polars.py#L44

Added line #L44 was not covered by tests
"""
Pandera generic for pl.Series, only used for type annotation.

*new in 0.19.0*
"""
19 changes: 18 additions & 1 deletion tests/polars/test_polars_dataframe_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,24 @@
import pytest

import pandera.polars as pa
from pandera.typing.polars import LazyFrame
from pandera.typing.polars import LazyFrame, Series


def test_series_annotation():
class Model(pa.DataFrameModel):
col1: Series[pl.Int64]

data = pl.LazyFrame(
{
"col1": [1, 2, 3],
}
)

assert data.collect().equals(Model.validate(data).collect())

invalid_data = data.cast({"col1": pl.Float64})
with pytest.raises(pa.errors.SchemaError):
Model.validate(invalid_data).collect()


def test_lazyframe_generic_simple():
Expand Down
Loading