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

clean: (partial) factor test_common #482

Merged
merged 4 commits into from
Jul 10, 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
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def pandas_constructor(obj: Any) -> IntoDataFrame:


def pandas_nullable_constructor(obj: Any) -> IntoDataFrame:
return pd.DataFrame(obj).convert_dtypes() # type: ignore[no-any-return]
return pd.DataFrame(obj).convert_dtypes(dtype_backend="numpy_nullable") # type: ignore[no-any-return]


def pandas_pyarrow_constructor(obj: Any) -> IntoDataFrame:
Expand Down
21 changes: 21 additions & 0 deletions tests/frame/head_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from __future__ import annotations

from typing import Any

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


def test_head(constructor_with_lazy: Any) -> None:
data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}
df_raw = constructor_with_lazy(data)
df = nw.from_native(df_raw).lazy()
result = nw.to_native(df.head(2))
expected = {"a": [1, 3], "b": [4, 4], "z": [7.0, 8.0]}
compare_dicts(result, expected)
result = nw.to_native(df.collect().head(2))
expected = {"a": [1, 3], "b": [4, 4], "z": [7.0, 8.0]}
compare_dicts(result, expected)
result = nw.to_native(df.collect().select(nw.col("a").head(2)))
expected = {"a": [1, 3]}
compare_dicts(result, expected)
16 changes: 16 additions & 0 deletions tests/frame/is_duplicated_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import annotations

from typing import Any

import numpy as np

import narwhals.stable.v1 as nw


def test_is_duplicated(constructor: Any) -> None:
data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}
df_raw = constructor(data)
df = nw.from_native(df_raw, eager_only=True)
result = nw.concat([df, df.head(1)]).is_duplicated()
expected = np.array([True, False, False, True])
assert (result.to_numpy() == expected).all()
16 changes: 16 additions & 0 deletions tests/frame/is_empty_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import annotations

from typing import Any

import pytest

import narwhals.stable.v1 as nw


@pytest.mark.parametrize(("threshold", "expected"), [(0, False), (10, True)])
def test_is_empty(constructor: Any, threshold: Any, expected: Any) -> None:
data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}
df_raw = constructor(data)
df = nw.from_native(df_raw, eager_only=True)
result = df.filter(nw.col("a") > threshold).is_empty()
assert result == expected
16 changes: 16 additions & 0 deletions tests/frame/is_unique_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import annotations

from typing import Any

import numpy as np

import narwhals.stable.v1 as nw


def test_is_unique(constructor: Any) -> None:
data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}
df_raw = constructor(data)
df = nw.from_native(df_raw, eager_only=True)
result = nw.concat([df, df.head(1)]).is_unique()
expected = np.array([False, True, True, False])
assert (result.to_numpy() == expected).all()
52 changes: 52 additions & 0 deletions tests/frame/lit_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import Any

import numpy as np
import pytest

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

if TYPE_CHECKING:
from narwhals.dtypes import DType


@pytest.mark.parametrize(
("dtype", "expected_lit"),
[(None, [2, 2, 2]), (nw.String, ["2", "2", "2"]), (nw.Float32, [2.0, 2.0, 2.0])],
)
def test_lit(
constructor_with_lazy: Any, dtype: DType | None, expected_lit: list[Any]
) -> None:
data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}
df_raw = constructor_with_lazy(data)
df = nw.from_native(df_raw)
result = df.with_columns(nw.lit(2, dtype).alias("lit"))
result_native = nw.to_native(result)
expected = {
"a": [1, 3, 2],
"b": [4, 4, 6],
"z": [7.0, 8.0, 9.0],
"lit": expected_lit,
}
compare_dicts(result_native, expected)


def test_lit_error(constructor_with_lazy: Any) -> None:
data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}
df_raw = constructor_with_lazy(data)
df = nw.from_native(df_raw)
with pytest.raises(
ValueError, match="numpy arrays are not supported as literal values"
):
_ = df.with_columns(nw.lit(np.array([1, 2])).alias("lit"))
with pytest.raises(
NotImplementedError, match="Nested datatypes are not supported yet."
):
_ = df.with_columns(nw.lit((1, 2)).alias("lit"))
with pytest.raises(
NotImplementedError, match="Nested datatypes are not supported yet."
):
_ = df.with_columns(nw.lit([1, 2]).alias("lit"))
15 changes: 15 additions & 0 deletions tests/frame/null_count_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from __future__ import annotations

from typing import Any

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


def test_null_count(constructor: Any) -> None:
data = {"a": [None, 3, 2], "b": [4, 4, 6], "z": [7.0, None, 9]}
df_raw = constructor(data)
df = nw.from_native(df_raw, eager_only=True)
result = nw.to_native(df.null_count())
expected = {"a": [1], "b": [0], "z": [1]}
compare_dicts(result, expected)
36 changes: 36 additions & 0 deletions tests/frame/quantile_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from __future__ import annotations

from typing import Any
from typing import Literal

import pytest

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


@pytest.mark.parametrize(
("interpolation", "expected"),
[
("lower", {"a": [1.0], "b": [4.0], "z": [7.0]}),
("higher", {"a": [2.0], "b": [4.0], "z": [8.0]}),
("midpoint", {"a": [1.5], "b": [4.0], "z": [7.5]}),
("linear", {"a": [1.6], "b": [4.0], "z": [7.6]}),
("nearest", {"a": [2.0], "b": [4.0], "z": [8.0]}),
],
)
@pytest.mark.filterwarnings("ignore:the `interpolation=` argument to percentile")
def test_quantile(
constructor: Any,
interpolation: Literal["nearest", "higher", "lower", "midpoint", "linear"],
expected: dict[str, list[float]],
) -> None:
q = 0.3

data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}
df_raw = constructor(data)
df = nw.from_native(df_raw)
result = nw.to_native(
df.select(nw.all().quantile(quantile=q, interpolation=interpolation))
)
compare_dicts(result, expected)
21 changes: 21 additions & 0 deletions tests/frame/tail_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from __future__ import annotations

from typing import Any

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


def test_tail(constructor_with_lazy: Any) -> None:
data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}
df_raw = constructor_with_lazy(data)
df = nw.from_native(df_raw).lazy()
result = nw.to_native(df.tail(2))
expected = {"a": [3, 2], "b": [4, 6], "z": [8.0, 9]}
compare_dicts(result, expected)
result = nw.to_native(df.collect().tail(2))
expected = {"a": [3, 2], "b": [4, 6], "z": [8.0, 9]}
compare_dicts(result, expected)
result = nw.to_native(df.collect().select(nw.col("a").tail(2)))
expected = {"a": [3, 2]}
compare_dicts(result, expected)
Loading
Loading