Skip to content

Commit

Permalink
chore: remove some pyarrow xfails from tests, further split up (#660)
Browse files Browse the repository at this point in the history
* wi

* go further
  • Loading branch information
MarcoGorelli authored Jul 28, 2024
1 parent cfcd653 commit 74cbdea
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 34 deletions.
8 changes: 6 additions & 2 deletions narwhals/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,9 @@ def from_native( # noqa: PLR0915
)

# Modin
elif (mpd := get_modin()) is not None and isinstance(native_object, mpd.DataFrame):
elif (mpd := get_modin()) is not None and isinstance(
native_object, mpd.DataFrame
): # pragma: no cover
if series_only:
msg = "Cannot only use `series_only` with modin.DataFrame"
raise TypeError(msg)
Expand All @@ -408,7 +410,9 @@ def from_native( # noqa: PLR0915
),
level="full",
)
elif (mpd := get_modin()) is not None and isinstance(native_object, mpd.Series):
elif (mpd := get_modin()) is not None and isinstance(
native_object, mpd.Series
): # pragma: no cover
if not allow_series:
msg = "Please set `allow_series=True`"
raise TypeError(msg)
Expand Down
19 changes: 19 additions & 0 deletions tests/expr_and_series/any_all_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,22 @@ def test_any_all(constructor: Any) -> None:
result = df.select(nw.all().any())
expected = {"a": [True], "b": [True], "c": [False]}
compare_dicts(result, expected)


def test_any_all_series(constructor_eager: Any) -> None:
df = nw.from_native(
constructor_eager(
{
"a": [True, False, True],
"b": [True, True, True],
"c": [False, False, False],
}
),
eager_only=True,
)
result = {"a": [df["a"].all()], "b": [df["b"].all()], "c": [df["c"].all()]}
expected = {"a": [False], "b": [True], "c": [False]}
compare_dicts(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)
8 changes: 8 additions & 0 deletions tests/expr_and_series/count_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ def test_count(constructor: Any) -> None:
result = df.select(nw.col("a", "b", "z").count())
expected = {"a": [3], "b": [2], "z": [1]}
compare_dicts(result, expected)


def test_count_series(constructor_eager: Any) -> None:
data = {"a": [1, 3, 2], "b": [4, None, 6], "z": [7.0, None, None]}
df = nw.from_native(constructor_eager(data), eager_only=True)
result = {"a": [df["a"].count()], "b": [df["b"].count()], "z": [df["z"].count()]}
expected = {"a": [3], "b": [2], "z": [1]}
compare_dicts(result, expected)
19 changes: 19 additions & 0 deletions tests/frame/std_test.py → tests/expr_and_series/std_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,22 @@ def test_std(constructor: Any) -> None:
"z_ddof_0": [0.816497],
}
compare_dicts(result, expected)


def test_std_series(constructor_eager: Any) -> None:
df = nw.from_native(constructor_eager(data), eager_only=True)
result = {
"a_ddof_default": [df["a"].std()],
"a_ddof_1": [df["a"].std(ddof=1)],
"a_ddof_0": [df["a"].std(ddof=0)],
"b_ddof_2": [df["b"].std(ddof=2)],
"z_ddof_0": [df["z"].std(ddof=0)],
}
expected = {
"a_ddof_default": [1.0],
"a_ddof_1": [1.0],
"a_ddof_0": [0.816497],
"b_ddof_2": [1.632993],
"z_ddof_0": [0.816497],
}
compare_dicts(result, expected)
25 changes: 25 additions & 0 deletions tests/series_only/is_between_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from __future__ import annotations

from typing import Any

import pytest

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

data = [1, 4, 2, 5]


@pytest.mark.parametrize(
("closed", "expected"),
[
("left", [True, True, True, False]),
("right", [False, True, True, True]),
("both", [True, True, True, True]),
("none", [False, True, True, False]),
],
)
def test_is_between(constructor_eager: Any, closed: str, expected: list[bool]) -> None:
ser = nw.from_native(constructor_eager({"a": data}), eager_only=True)["a"]
result = ser.is_between(1, 5, closed=closed)
compare_dicts({"a": result}, {"a": expected})
32 changes: 0 additions & 32 deletions tests/series_only/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,6 @@ def test_dtype(constructor_eager: Any) -> None:
assert result.is_numeric()


def test_reductions(request: Any, constructor_eager: Any) -> None:
if "pyarrow_table" in str(constructor_eager):
request.applymarker(pytest.mark.xfail)

s = nw.from_native(constructor_eager({"a": data}), eager_only=True)["a"]
assert s.mean() == 2.0
assert s.std() == 1.0
assert s.min() == 1
assert s.max() == 3
assert s.count() == 3
assert s.sum() == 6
assert nw.to_native(s.is_between(1, 2))[0]
assert not nw.to_native(s.is_between(1, 2))[1]
assert nw.to_native(s.is_between(1, 2))[2]
assert s.n_unique() == 3
unique = s.unique().sort()
assert unique[0] == 1
assert unique[1] == 2
assert unique[2] == 3
assert s.alias("foo").name == "foo"


def test_boolean_reductions(request: Any, constructor_eager: Any) -> None:
if "pyarrow_table" in str(constructor_eager):
request.applymarker(pytest.mark.xfail)

df_raw = constructor_eager({"a": data})
df = nw.from_native(df_raw).lazy().select(nw.col("a") > 1)
assert not df.collect()["a"].all()
assert df.collect()["a"].any()


@pytest.mark.skipif(
parse_version(pd.__version__) < parse_version("2.0.0"), reason="too old for pyarrow"
)
Expand Down

0 comments on commit 74cbdea

Please sign in to comment.