Skip to content

Commit

Permalink
chore: Make parametric tests include pl.List and pl.Array by defa…
Browse files Browse the repository at this point in the history
…ult (#20319)

Co-authored-by: ritchie <[email protected]>
  • Loading branch information
coastalwhite and ritchie46 authored Dec 17, 2024
1 parent e03555c commit 6142ee5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
6 changes: 6 additions & 0 deletions py-polars/tests/unit/interchange/test_roundtrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
from polars.testing import assert_frame_equal, assert_series_equal
from polars.testing.parametric import dataframes

skip_if_broken_pandas_version = pytest.mark.skipif(
pd.__version__.startswith("2"), reason="bug. see #20316"
)

if TYPE_CHECKING:
from polars._typing import PolarsDataType

Expand Down Expand Up @@ -140,6 +144,7 @@ def test_from_dataframe_pyarrow_zero_copy_parametric(df: pl.DataFrame) -> None:
assert_frame_equal(result, df)


@skip_if_broken_pandas_version
@given(
dataframes(
allowed_dtypes=protocol_dtypes,
Expand All @@ -156,6 +161,7 @@ def test_from_dataframe_pandas_parametric(df: pl.DataFrame) -> None:
assert_frame_equal(result, df, categorical_as_str=True)


@skip_if_broken_pandas_version
@given(
dataframes(
allowed_dtypes=protocol_dtypes,
Expand Down
1 change: 1 addition & 0 deletions py-polars/tests/unit/test_row_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def roundtrip_series_re(
df=dataframes(
excluded_dtypes=[
pl.Categorical,
pl.Decimal, # Bug: see https://github.com/pola-rs/polars/issues/20308
]
)
)
Expand Down
48 changes: 40 additions & 8 deletions py-polars/tests/unit/test_row_encoding_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import datetime
import decimal
import functools
from typing import Any, Literal, Optional, Union
from typing import Any, Literal, Optional, Union, cast

import pytest
from hypothesis import given
from hypothesis import example, given

import polars as pl
from polars.testing import assert_frame_equal, assert_series_equal
Expand Down Expand Up @@ -36,18 +36,36 @@ def elem_order_sign(
lhs: Element, rhs: Element, *, descending: bool, nulls_last: bool
) -> OrderSign:
if isinstance(lhs, pl.Series) and isinstance(rhs, pl.Series):
if lhs.equals(rhs):
assert lhs.dtype == rhs.dtype

if isinstance(lhs.dtype, pl.Enum) or lhs.dtype == pl.Categorical(
ordering="physical"
):
lhs = cast(Element, lhs.to_physical())
rhs = cast(Element, rhs.to_physical())
assert isinstance(lhs, pl.Series)
assert isinstance(rhs, pl.Series)

if lhs.dtype == pl.Categorical(ordering="lexical"):
lhs = cast(Element, lhs.cast(pl.String))
rhs = cast(Element, rhs.cast(pl.String))
assert isinstance(lhs, pl.Series)
assert isinstance(rhs, pl.Series)

if lhs.is_null().equals(rhs.is_null()) and lhs.equals(rhs):
return 0

lhs = list(lhs)
rhs = list(rhs)
lhs = lhs.to_list()
rhs = rhs.to_list()

if lhs == rhs:
if lhs is None and rhs is None:
return 0
elif lhs is None:
return 1 if nulls_last else -1
elif rhs is None:
return -1 if nulls_last else 1
elif lhs == rhs:
return 0
elif isinstance(lhs, bool) and isinstance(rhs, bool):
return -1 if (lhs < rhs) ^ descending else 1
elif isinstance(lhs, datetime.date) and isinstance(rhs, datetime.date):
Expand Down Expand Up @@ -121,10 +139,16 @@ def tuple_order(

@given(
s=series(
excluded_dtypes=[pl.Categorical],
excluded_dtypes=[
pl.Float32, # We cannot really deal with totalOrder
pl.Float64, # We cannot really deal with totalOrder
pl.Decimal, # Bug: see https://github.com/pola-rs/polars/issues/20308
pl.Categorical,
],
max_size=5,
)
)
@example(s=pl.Series("col0", [None, [None]], pl.List(pl.Int64)))
def test_series_sort_parametric(s: pl.Series) -> None:
for descending in [False, True]:
for nulls_last in [False, True]:
Expand Down Expand Up @@ -154,7 +178,15 @@ def cmp(

@given(
df=dataframes(
excluded_dtypes=[pl.Categorical],
excluded_dtypes=[
pl.Float32, # We cannot really deal with totalOrder
pl.Float64, # We cannot really deal with totalOrder
pl.Decimal, # Bug: see https://github.com/pola-rs/polars/issues/20308
pl.List, # I am not sure what is broken here.
pl.Array, # I am not sure what is broken here.
pl.Enum,
pl.Categorical,
],
max_cols=3,
max_size=5,
)
Expand Down

0 comments on commit 6142ee5

Please sign in to comment.