Skip to content

Commit

Permalink
test(python): Remove verify_series_and_expr_api util (pola-rs#6524)
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego authored and cojmeister committed Jan 30, 2023
1 parent f006415 commit 34fc1aa
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 219 deletions.
26 changes: 0 additions & 26 deletions py-polars/polars/testing/_private.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,6 @@
from __future__ import annotations

from typing import Any

import polars.internals as pli
from polars.testing.asserts import _getattr_multi, assert_series_equal


def verify_series_and_expr_api(
input: pli.Series, expected: pli.Series | None, op: str, *args: Any, **kwargs: Any
) -> None:
"""
Test element-wise functions for both the series and expressions API.
Examples
--------
>>> s = pl.Series([1, 3, 2])
>>> expected = pl.Series([1, 2, 3])
>>> verify_series_and_expr_api(s, expected, "sort")
"""
expr = _getattr_multi(pli.col("*"), op)(*args, **kwargs)
result_expr = input.to_frame().select(expr)[:, 0]
result_series = _getattr_multi(input, op)(*args, **kwargs)
if expected is None:
assert_series_equal(result_series, result_expr)
else:
assert_series_equal(result_expr, expected)
assert_series_equal(result_series, expected)


def _to_rust_syntax(df: pli.DataFrame) -> str:
Expand Down
13 changes: 0 additions & 13 deletions py-polars/polars/testing/asserts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

from functools import reduce
from typing import Any

import polars.internals as pli
Expand Down Expand Up @@ -380,18 +379,6 @@ def raise_assert_detail(
raise AssertionError(msg)


def _getattr_multi(obj: object, op: str) -> Any:
"""
Allow `op` to be multiple layers deep.
For example, op="str.lengths" will mean we first get the attribute "str", and then
the attribute "lengths".
"""
op_list = op.split(".")
return reduce(lambda o, m: getattr(o, m), op_list, obj)


def is_categorical_dtype(data_type: Any) -> bool:
"""Check if the input is a polars Categorical dtype."""
return (
Expand Down
15 changes: 6 additions & 9 deletions py-polars/tests/unit/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import polars as pl
from polars.datatypes import DATETIME_DTYPES, DTYPE_TEMPORAL_UNITS, PolarsTemporalType
from polars.testing import assert_frame_equal, assert_series_equal
from polars.testing._private import verify_series_and_expr_api

if TYPE_CHECKING:
from polars.internals.type_aliases import TimeUnit
Expand Down Expand Up @@ -727,24 +726,22 @@ def test_to_arrow() -> None:


def test_non_exact_strptime() -> None:
a = pl.Series("a", ["2022-01-16", "2022-01-17", "foo2022-01-18", "b2022-01-19ar"])
s = pl.Series("a", ["2022-01-16", "2022-01-17", "foo2022-01-18", "b2022-01-19ar"])
fmt = "%Y-%m-%d"

result = s.str.strptime(pl.Date, fmt, strict=False, exact=True)
expected = pl.Series("a", [date(2022, 1, 16), date(2022, 1, 17), None, None])
verify_series_and_expr_api(
a, expected, "str.strptime", pl.Date, fmt, strict=False, exact=True
)
assert_series_equal(result, expected)

result = s.str.strptime(pl.Date, fmt, strict=False, exact=False)
expected = pl.Series(
"a",
[date(2022, 1, 16), date(2022, 1, 17), date(2022, 1, 18), date(2022, 1, 19)],
)
verify_series_and_expr_api(
a, expected, "str.strptime", pl.Date, fmt, strict=False, exact=False
)
assert_series_equal(result, expected)

with pytest.raises(Exception):
a.str.strptime(pl.Date, fmt, strict=True, exact=True)
s.str.strptime(pl.Date, fmt, strict=True, exact=True)


def test_explode_date() -> None:
Expand Down
10 changes: 4 additions & 6 deletions py-polars/tests/unit/test_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
TEMPORAL_DTYPES,
)
from polars.testing import assert_series_equal
from polars.testing._private import verify_series_and_expr_api


def test_horizontal_agg(fruits_cars: pl.DataFrame) -> None:
Expand Down Expand Up @@ -79,7 +78,7 @@ def test_min_nulls_consistency() -> None:
def test_list_join_strings() -> None:
s = pl.Series("a", [["ab", "c", "d"], ["e", "f"], ["g"], []])
expected = pl.Series("a", ["ab-c-d", "e-f", "g", ""])
verify_series_and_expr_api(s, expected, "arr.join", "-")
assert_series_equal(s.arr.join("-"), expected)


def test_count_expr() -> None:
Expand Down Expand Up @@ -137,10 +136,9 @@ def test_map_alias() -> None:


def test_unique_stable() -> None:
a = pl.Series("a", [1, 1, 1, 1, 2, 2, 2, 3, 3])
s = pl.Series("a", [1, 1, 1, 1, 2, 2, 2, 3, 3])
expected = pl.Series("a", [1, 2, 3])

verify_series_and_expr_api(a, expected, "unique", True)
assert_series_equal(s.unique(maintain_order=True), expected)


def test_wildcard_expansion() -> None:
Expand Down Expand Up @@ -241,7 +239,7 @@ def test_unique_and_drop_stability() -> None:
def test_unique_counts() -> None:
s = pl.Series("id", ["a", "b", "b", "c", "c", "c"])
expected = pl.Series("id", [1, 2, 3], dtype=pl.UInt32)
verify_series_and_expr_api(s, expected, "unique_counts")
assert_series_equal(s.unique_counts(), expected)


def test_entropy() -> None:
Expand Down
5 changes: 2 additions & 3 deletions py-polars/tests/unit/test_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import polars as pl
from polars.testing import assert_series_equal
from polars.testing._private import verify_series_and_expr_api


def test_list_arr_get() -> None:
Expand Down Expand Up @@ -243,9 +242,9 @@ def test_list_arr_empty() -> None:
def test_list_argminmax() -> None:
s = pl.Series("a", [[1, 2], [3, 2, 1]])
expected = pl.Series("a", [0, 2], dtype=pl.UInt32)
verify_series_and_expr_api(s, expected, "arr.arg_min")
assert_series_equal(s.arr.arg_min(), expected)
expected = pl.Series("a", [1, 0], dtype=pl.UInt32)
verify_series_and_expr_api(s, expected, "arr.arg_max")
assert_series_equal(s.arr.arg_max(), expected)


def test_list_shift() -> None:
Expand Down
Loading

0 comments on commit 34fc1aa

Please sign in to comment.