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

chore: refactor compare_dicts #1224

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions tests/expr_and_series/abs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
from tests.utils import compare_dicts
from tests.utils import assert_equal_data


def test_abs(constructor: Constructor) -> None:
df = nw.from_native(constructor({"a": [1, 2, 3, -4, 5]}))
result = df.select(b=nw.col("a").abs())
expected = {"b": [1, 2, 3, 4, 5]}
compare_dicts(result, expected)
assert_equal_data(result, expected)


def test_abs_series(constructor_eager: ConstructorEager) -> None:
df = nw.from_native(constructor_eager({"a": [1, 2, 3, -4, 5]}), eager_only=True)
result = {"b": df["a"].abs()}
expected = {"b": [1, 2, 3, 4, 5]}
compare_dicts(result, expected)
assert_equal_data(result, expected)
14 changes: 7 additions & 7 deletions tests/expr_and_series/all_horizontal_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from narwhals.utils import parse_version
from tests.utils import Constructor
from tests.utils import ConstructorEager
from tests.utils import compare_dicts
from tests.utils import assert_equal_data


@pytest.mark.parametrize("expr1", ["a", nw.col("a")])
Expand All @@ -23,7 +23,7 @@ def test_allh(constructor: Constructor, expr1: Any, expr2: Any) -> None:
result = df.select(all=nw.all_horizontal(expr1, expr2))

expected = {"all": [False, False, True]}
compare_dicts(result, expected)
assert_equal_data(result, expected)


def test_allh_series(constructor_eager: ConstructorEager) -> None:
Expand All @@ -35,7 +35,7 @@ def test_allh_series(constructor_eager: ConstructorEager) -> None:
result = df.select(all=nw.all_horizontal(df["a"], df["b"]))

expected = {"all": [False, False, True]}
compare_dicts(result, expected)
assert_equal_data(result, expected)


def test_allh_all(constructor: Constructor) -> None:
Expand All @@ -46,10 +46,10 @@ def test_allh_all(constructor: Constructor) -> None:
df = nw.from_native(constructor(data))
result = df.select(all=nw.all_horizontal(nw.all()))
expected = {"all": [False, False, True]}
compare_dicts(result, expected)
assert_equal_data(result, expected)
result = df.select(nw.all_horizontal(nw.all()))
expected = {"a": [False, False, True]}
compare_dicts(result, expected)
assert_equal_data(result, expected)


def test_allh_nth(constructor: Constructor, request: pytest.FixtureRequest) -> None:
Expand All @@ -62,10 +62,10 @@ def test_allh_nth(constructor: Constructor, request: pytest.FixtureRequest) -> N
df = nw.from_native(constructor(data))
result = df.select(nw.all_horizontal(nw.nth(0, 1)))
expected = {"a": [False, False, True]}
compare_dicts(result, expected)
assert_equal_data(result, expected)
result = df.select(nw.all_horizontal(nw.col("a"), nw.nth(0)))
expected = {"a": [False, False, True]}
compare_dicts(result, expected)
assert_equal_data(result, expected)


def test_horizontal_expressions_empty(constructor: Constructor) -> None:
Expand Down
10 changes: 5 additions & 5 deletions tests/expr_and_series/any_all_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
from tests.utils import compare_dicts
from tests.utils import assert_equal_data


def test_any_all(constructor: Constructor) -> None:
Expand All @@ -18,10 +18,10 @@ def test_any_all(constructor: Constructor) -> None:
)
result = df.select(nw.col("a", "b", "c").all())
expected = {"a": [False], "b": [True], "c": [False]}
compare_dicts(result, expected)
assert_equal_data(result, expected)
result = df.select(nw.all().any())
expected = {"a": [True], "b": [True], "c": [False]}
compare_dicts(result, expected)
assert_equal_data(result, expected)


def test_any_all_series(constructor_eager: ConstructorEager) -> None:
Expand All @@ -37,7 +37,7 @@ def test_any_all_series(constructor_eager: ConstructorEager) -> None:
)
result = {"a": [df["a"].all()], "b": [df["b"].all()], "c": [df["c"].all()]}
expected = {"a": [False], "b": [True], "c": [False]}
compare_dicts(result, expected)
assert_equal_data(result, expected)
result = {"a": [df["a"].any()], "b": [df["b"].any()], "c": [df["c"].any()]}
expected = {"a": [True], "b": [True], "c": [False]}
compare_dicts(result, expected)
assert_equal_data(result, expected)
8 changes: 4 additions & 4 deletions tests/expr_and_series/any_horizontal_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import compare_dicts
from tests.utils import assert_equal_data


@pytest.mark.parametrize("expr1", ["a", nw.col("a")])
Expand All @@ -20,7 +20,7 @@ def test_anyh(constructor: Constructor, expr1: Any, expr2: Any) -> None:
result = df.select(any=nw.any_horizontal(expr1, expr2))

expected = {"any": [False, True, True]}
compare_dicts(result, expected)
assert_equal_data(result, expected)


def test_anyh_all(constructor: Constructor) -> None:
Expand All @@ -31,7 +31,7 @@ def test_anyh_all(constructor: Constructor) -> None:
df = nw.from_native(constructor(data))
result = df.select(any=nw.any_horizontal(nw.all()))
expected = {"any": [False, True, True]}
compare_dicts(result, expected)
assert_equal_data(result, expected)
result = df.select(nw.any_horizontal(nw.all()))
expected = {"a": [False, True, True]}
compare_dicts(result, expected)
assert_equal_data(result, expected)
6 changes: 3 additions & 3 deletions tests/expr_and_series/arg_true_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
from tests.utils import compare_dicts
from tests.utils import assert_equal_data


def test_arg_true(constructor: Constructor, request: pytest.FixtureRequest) -> None:
Expand All @@ -14,12 +14,12 @@ def test_arg_true(constructor: Constructor, request: pytest.FixtureRequest) -> N
df = nw.from_native(constructor({"a": [1, None, None, 3]}))
result = df.select(nw.col("a").is_null().arg_true())
expected = {"a": [1, 2]}
compare_dicts(result, expected)
assert_equal_data(result, expected)


def test_arg_true_series(constructor_eager: ConstructorEager) -> None:
df = nw.from_native(constructor_eager({"a": [1, None, None, 3]}), eager_only=True)
result = df.select(df["a"].is_null().arg_true())
expected = {"a": [1, 2]}
compare_dicts(result, expected)
assert_equal_data(result, expected)
assert "a" in df # cheeky test to hit `__contains__` method
32 changes: 16 additions & 16 deletions tests/expr_and_series/arithmetic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from narwhals.utils import parse_version
from tests.utils import Constructor
from tests.utils import ConstructorEager
from tests.utils import compare_dicts
from tests.utils import assert_equal_data


@pytest.mark.parametrize(
Expand Down Expand Up @@ -45,7 +45,7 @@ def test_arithmetic_expr(
data = {"a": [1.0, 2, 3]}
df = nw.from_native(constructor(data))
result = df.select(getattr(nw.col("a"), attr)(rhs))
compare_dicts(result, {"a": expected})
assert_equal_data(result, {"a": expected})


@pytest.mark.parametrize(
Expand Down Expand Up @@ -75,7 +75,7 @@ def test_right_arithmetic_expr(
data = {"a": [1, 2, 3]}
df = nw.from_native(constructor(data))
result = df.select(a=getattr(nw.col("a"), attr)(rhs))
compare_dicts(result, {"a": expected})
assert_equal_data(result, {"a": expected})


@pytest.mark.parametrize(
Expand Down Expand Up @@ -106,7 +106,7 @@ def test_arithmetic_series(
data = {"a": [1, 2, 3]}
df = nw.from_native(constructor_eager(data), eager_only=True)
result = df.select(getattr(df["a"], attr)(rhs))
compare_dicts(result, {"a": expected})
assert_equal_data(result, {"a": expected})


@pytest.mark.parametrize(
Expand Down Expand Up @@ -136,7 +136,7 @@ def test_right_arithmetic_series(
data = {"a": [1, 2, 3]}
df = nw.from_native(constructor_eager(data), eager_only=True)
result = df.select(a=getattr(df["a"], attr)(rhs))
compare_dicts(result, {"a": expected})
assert_equal_data(result, {"a": expected})


def test_truediv_same_dims(
Expand All @@ -148,9 +148,9 @@ def test_truediv_same_dims(
s_left = nw.from_native(constructor_eager({"a": [1, 2, 3]}), eager_only=True)["a"]
s_right = nw.from_native(constructor_eager({"a": [2, 2, 1]}), eager_only=True)["a"]
result = s_left / s_right
compare_dicts({"a": result}, {"a": [0.5, 1.0, 3.0]})
assert_equal_data({"a": result}, {"a": [0.5, 1.0, 3.0]})
result = s_left.__rtruediv__(s_right)
compare_dicts({"a": result}, {"a": [2, 1, 1 / 3]})
assert_equal_data({"a": result}, {"a": [2, 1, 1 / 3]})


@pytest.mark.slow
Expand All @@ -169,7 +169,7 @@ def test_floordiv(left: int, right: int) -> None:
result = nw.from_native(pd.DataFrame({"a": [left]}), eager_only=True).select(
nw.col("a") // right
)
compare_dicts(result, expected)
assert_equal_data(result, expected)
if parse_version(pd.__version__) < (2, 2): # pragma: no cover
# Bug in old version of pandas
pass
Expand All @@ -178,19 +178,19 @@ def test_floordiv(left: int, right: int) -> None:
pd.DataFrame({"a": [left]}).convert_dtypes(dtype_backend="pyarrow"),
eager_only=True,
).select(nw.col("a") // right)
compare_dicts(result, expected)
assert_equal_data(result, expected)
result = nw.from_native(
pd.DataFrame({"a": [left]}).convert_dtypes(), eager_only=True
).select(nw.col("a") // right)
compare_dicts(result, expected)
assert_equal_data(result, expected)
result = nw.from_native(pl.DataFrame({"a": [left]}), eager_only=True).select(
nw.col("a") // right
)
compare_dicts(result, expected)
assert_equal_data(result, expected)
result = nw.from_native(pa.table({"a": [left]}), eager_only=True).select(
nw.col("a") // right
)
compare_dicts(result, expected)
assert_equal_data(result, expected)


@pytest.mark.slow
Expand All @@ -209,16 +209,16 @@ def test_mod(left: int, right: int) -> None:
result = nw.from_native(pd.DataFrame({"a": [left]}), eager_only=True).select(
nw.col("a") % right
)
compare_dicts(result, expected)
assert_equal_data(result, expected)
result = nw.from_native(
pd.DataFrame({"a": [left]}).convert_dtypes(), eager_only=True
).select(nw.col("a") % right)
compare_dicts(result, expected)
assert_equal_data(result, expected)
result = nw.from_native(pl.DataFrame({"a": [left]}), eager_only=True).select(
nw.col("a") % right
)
compare_dicts(result, expected)
assert_equal_data(result, expected)
result = nw.from_native(pa.table({"a": [left]}), eager_only=True).select(
nw.col("a") % right
)
compare_dicts(result, expected)
assert_equal_data(result, expected)
4 changes: 2 additions & 2 deletions tests/expr_and_series/binary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import compare_dicts
from tests.utils import assert_equal_data


def test_expr_binary(constructor: Constructor) -> None:
Expand Down Expand Up @@ -43,4 +43,4 @@ def test_expr_binary(constructor: Constructor) -> None:
"l": [0, 1, 1],
"m": [1, 9, 4],
}
compare_dicts(result, expected)
assert_equal_data(result, expected)
4 changes: 2 additions & 2 deletions tests/expr_and_series/cast_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import narwhals.stable.v1 as nw
from narwhals.utils import parse_version
from tests.utils import Constructor
from tests.utils import compare_dicts
from tests.utils import assert_equal_data
from tests.utils import is_windows

data = {
Expand Down Expand Up @@ -217,4 +217,4 @@ def test_cast_datetime_tz_aware(
.cast(nw.String())
.str.slice(offset=0, length=19)
)
compare_dicts(result, expected)
assert_equal_data(result, expected)
10 changes: 5 additions & 5 deletions tests/expr_and_series/cat/get_categories_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import narwhals.stable.v1 as nw
from narwhals.utils import parse_version
from tests.utils import ConstructorEager
from tests.utils import compare_dicts
from tests.utils import assert_equal_data

data = {"a": ["one", "two", "two"]}

Expand All @@ -24,10 +24,10 @@ def test_get_categories(
expected = {"a": ["one", "two"]}

result_expr = df.select(nw.col("a").cat.get_categories())
compare_dicts(result_expr, expected)
assert_equal_data(result_expr, expected)

result_series = df["a"].cat.get_categories()
compare_dicts({"a": result_series}, expected)
assert_equal_data({"a": result_series}, expected)


def test_get_categories_pyarrow() -> None:
Expand All @@ -40,7 +40,7 @@ def test_get_categories_pyarrow() -> None:
expected = {"a": ["a", "b", "d"]}

result_expr = df.select(nw.col("a").cat.get_categories())
compare_dicts(result_expr, expected)
assert_equal_data(result_expr, expected)

result_series = df["a"].cat.get_categories()
compare_dicts({"a": result_series}, expected)
assert_equal_data({"a": result_series}, expected)
6 changes: 3 additions & 3 deletions tests/expr_and_series/clip_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
from tests.utils import compare_dicts
from tests.utils import assert_equal_data


def test_clip(constructor: Constructor) -> None:
Expand All @@ -18,7 +18,7 @@ def test_clip(constructor: Constructor) -> None:
"upper_only": [1, 2, 3, -4, 4],
"both": [3, 3, 3, 3, 4],
}
compare_dicts(result, expected)
assert_equal_data(result, expected)


def test_clip_series(constructor_eager: ConstructorEager) -> None:
Expand All @@ -34,4 +34,4 @@ def test_clip_series(constructor_eager: ConstructorEager) -> None:
"upper_only": [1, 2, 3, -4, 4],
"both": [3, 3, 3, 3, 4],
}
compare_dicts(result, expected)
assert_equal_data(result, expected)
6 changes: 3 additions & 3 deletions tests/expr_and_series/concat_str_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import compare_dicts
from tests.utils import assert_equal_data

data = {
"a": [1, 2, 3],
Expand Down Expand Up @@ -40,7 +40,7 @@ def test_concat_str(
.sort("a")
.select("full_sentence")
)
compare_dicts(result, {"full_sentence": expected})
assert_equal_data(result, {"full_sentence": expected})
result = (
df.select(
"a",
Expand All @@ -55,4 +55,4 @@ def test_concat_str(
.sort("a")
.select("full_sentence")
)
compare_dicts(result, {"full_sentence": expected})
assert_equal_data(result, {"full_sentence": expected})
Loading
Loading