-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathany_horizontal_test.py
More file actions
109 lines (90 loc) · 4.04 KB
/
any_horizontal_test.py
File metadata and controls
109 lines (90 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from __future__ import annotations
from contextlib import nullcontext as does_not_raise
from typing import TYPE_CHECKING
import pytest
import narwhals as nw
from tests.utils import Constructor, assert_equal_data
if TYPE_CHECKING:
from narwhals.typing import PythonLiteral
def test_anyh(constructor: Constructor) -> None:
data = {"a": [False, False, True], "b": [False, True, True]}
df = nw.from_native(constructor(data))
result = df.select(any=nw.any_horizontal(nw.col("a"), "b", ignore_nulls=True))
expected = {"any": [False, True, True]}
assert_equal_data(result, expected)
def test_anyh_kleene(constructor: Constructor, request: pytest.FixtureRequest) -> None:
if "cudf" in str(constructor):
# https://github.com/rapidsai/cudf/issues/19171
request.applymarker(pytest.mark.xfail)
if "dask" in str(constructor):
# Dask infers `[True, None, None, None]` as `object` dtype, and then `__or__` fails.
# test it below separately
pytest.skip()
context = (
pytest.raises(ValueError, match="ignore_nulls")
if "pandas_constructor" in str(constructor)
else does_not_raise()
)
data = {"a": [True, True, False], "b": [True, None, None]}
df = nw.from_native(constructor(data))
with context:
result = df.select(any=nw.any_horizontal("a", "b", ignore_nulls=False))
expected = [True, True, None]
assert_equal_data(result, {"any": expected})
def test_anyh_ignore_nulls(constructor: Constructor) -> None:
if "dask" in str(constructor):
# Dask infers `[True, None, None, None]` as `object` dtype, and then `__or__` fails.
# test it below separately
pytest.skip()
data = {"a": [True, True, False], "b": [True, None, None]}
df = nw.from_native(constructor(data))
result = df.select(any=nw.any_horizontal("a", "b", ignore_nulls=True))
expected = [True, True, False]
assert_equal_data(result, {"any": expected})
def test_anyh_dask(constructor: Constructor) -> None:
if "dask" not in str(constructor):
pytest.skip()
import dask.dataframe as dd
import pandas as pd
data = {"a": [True, True, False], "b": [True, None, None]}
df = nw.from_native(dd.from_pandas(pd.DataFrame(data, dtype="Boolean[pyarrow]")))
result = df.select(any=nw.any_horizontal("a", "b", ignore_nulls=True))
expected: list[bool | None] = [True, True, False]
assert_equal_data(result, {"any": expected})
result = df.select(any=nw.any_horizontal("a", "b", ignore_nulls=False))
expected = [True, True, None]
assert_equal_data(result, {"any": expected})
# No nulls, NumPy-backed
data = {"a": [True, True, False], "b": [True, False, False]}
df = nw.from_native(dd.from_pandas(pd.DataFrame(data)))
result = df.select(any=nw.any_horizontal("a", "b", ignore_nulls=True))
expected = [True, True, False]
assert_equal_data(result, {"any": expected})
def test_anyh_all(constructor: Constructor) -> None:
if "dask" in str(constructor):
# Can't use `ignore_nulls` for NumPy-backed Dask, test it separately below
pytest.skip()
data = {"a": [False, False, True], "b": [False, True, True]}
df = nw.from_native(constructor(data))
result = df.select(any=nw.any_horizontal(nw.all(), ignore_nulls=False))
expected = {"any": [False, True, True]}
assert_equal_data(result, expected)
result = df.select(nw.any_horizontal(nw.all(), ignore_nulls=False))
expected = {"a": [False, True, True]}
assert_equal_data(result, expected)
@pytest.mark.parametrize(
("exprs", "name"),
[
((nw.col("a"), False), "a"),
((nw.col("a"), nw.lit(False)), "a"),
((False, nw.col("a")), "literal"),
((nw.lit(False), nw.col("a")), "literal"),
],
)
def test_anyh_with_scalars(
constructor: Constructor, exprs: tuple[PythonLiteral | nw.Expr, ...], name: str
) -> None:
data = {"a": [False, True]}
df = nw.from_native(constructor(data))
result = df.select(nw.any_horizontal(*exprs, ignore_nulls=True))
assert_equal_data(result, {name: [False, True]})