Skip to content

Commit

Permalink
test: dont convert everything to nan
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Jan 1, 2025
1 parent 1f9b59e commit 50037a5
Show file tree
Hide file tree
Showing 19 changed files with 96 additions and 77 deletions.
8 changes: 4 additions & 4 deletions tests/expr_and_series/fill_null_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def test_fill_null_limits(constructor: Constructor) -> None:
nw.col("a", "b").fill_null(strategy="forward", limit=2)
)
expected_forward = {
"a": [1, 1, 1, float("nan"), 5, 6, 6, 6, float("nan"), 10],
"a": [1, 1, 1, None, 5, 6, 6, 6, None, 10],
"b": ["a", "a", "a", None, "b", "c", "c", "c", None, "d"],
}
assert_equal_data(result_forward, expected_forward)
Expand All @@ -146,7 +146,7 @@ def test_fill_null_limits(constructor: Constructor) -> None:
)

expected_backward = {
"a": [1, float("nan"), 5, 5, 5, 6, float("nan"), 10, 10, 10],
"a": [1, None, 5, 5, 5, 6, None, 10, 10, 10],
"b": ["a", None, "b", "b", "b", "c", None, "d", "d", "d"],
}
assert_equal_data(result_backward, expected_backward)
Expand Down Expand Up @@ -203,7 +203,7 @@ def test_fill_null_series_limits(constructor_eager: ConstructorEager) -> None:
"ignore", message="The 'downcast' keyword in fillna is deprecated"
)
expected_forward = {
"a_forward": [0.0, 1, 1, float("nan"), 2, 2, float("nan"), 3],
"a_forward": [0.0, 1, 1, None, 2, 2, None, 3],
"b_forward": ["", "a", "a", None, "c", "c", None, "e"],
}
result_forward = df.select(
Expand All @@ -214,7 +214,7 @@ def test_fill_null_series_limits(constructor_eager: ConstructorEager) -> None:
assert_equal_data(result_forward, expected_forward)

expected_backward = {
"a_backward": [0.0, 1, float("nan"), 2, 2, float("nan"), 3, 3],
"a_backward": [0.0, 1, None, 2, 2, None, 3, 3],
"b_backward": ["", "a", None, "c", "c", None, "e", "e"],
}

Expand Down
2 changes: 1 addition & 1 deletion tests/expr_and_series/max_horizontal_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from tests.utils import assert_equal_data

data = {"a": [1, 3, None, None], "b": [4, None, 6, None], "z": [3, 1, None, None]}
expected_values = [4, 3, 6, float("nan")]
expected_values = [4, 3, 6, None]


@pytest.mark.parametrize("col_expr", [nw.col("a"), "a"])
Expand Down
2 changes: 1 addition & 1 deletion tests/expr_and_series/mean_horizontal_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_meanh(constructor: Constructor, col_expr: Any) -> None:
data = {"a": [1, 3, None, None], "b": [4, None, 6, None]}
df = nw.from_native(constructor(data))
result = df.select(horizontal_mean=nw.mean_horizontal(col_expr, nw.col("b")))
expected = {"horizontal_mean": [2.5, 3.0, 6.0, float("nan")]}
expected = {"horizontal_mean": [2.5, 3.0, 6.0, None]}
assert_equal_data(result, expected)


Expand Down
2 changes: 1 addition & 1 deletion tests/expr_and_series/min_horizontal_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from tests.utils import assert_equal_data

data = {"a": [1, 3, None, None], "b": [4, None, 6, None], "z": [3, 1, None, None]}
expected_values = [1, 1, 6, float("nan")]
expected_values = [1, 1, 6, None]


@pytest.mark.parametrize("col_expr", [nw.col("a"), "a"])
Expand Down
13 changes: 7 additions & 6 deletions tests/expr_and_series/rolling_mean_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import random
from typing import Any

import hypothesis.strategies as st
import pandas as pd
Expand All @@ -16,15 +17,15 @@

data = {"a": [None, 1, 2, None, 4, 6, 11]}

kwargs_and_expected = {
"x1": {"kwargs": {"window_size": 3}, "expected": [float("nan")] * 6 + [7.0]},
kwargs_and_expected: dict[str, dict[str, Any]] = {
"x1": {"kwargs": {"window_size": 3}, "expected": [None] * 6 + [7.0]},
"x2": {
"kwargs": {"window_size": 3, "min_periods": 1},
"expected": [float("nan"), 1.0, 1.5, 1.5, 3.0, 5.0, 7.0],
"expected": [None, 1.0, 1.5, 1.5, 3.0, 5.0, 7.0],
},
"x3": {
"kwargs": {"window_size": 2, "min_periods": 1},
"expected": [float("nan"), 1.0, 1.5, 2.0, 4.0, 5.0, 8.5],
"expected": [None, 1.0, 1.5, 2.0, 4.0, 5.0, 8.5],
},
"x4": {
"kwargs": {"window_size": 5, "min_periods": 1, "center": True},
Expand Down Expand Up @@ -52,7 +53,7 @@ def test_rolling_mean_expr(
df = nw.from_native(constructor(data))
result = df.select(
**{
name: nw.col("a").rolling_mean(**values["kwargs"]) # type: ignore[arg-type]
name: nw.col("a").rolling_mean(**values["kwargs"])
for name, values in kwargs_and_expected.items()
}
)
Expand All @@ -69,7 +70,7 @@ def test_rolling_mean_series(constructor_eager: ConstructorEager) -> None:

result = df.select(
**{
name: df["a"].rolling_mean(**values["kwargs"]) # type: ignore[arg-type]
name: df["a"].rolling_mean(**values["kwargs"])
for name, values in kwargs_and_expected.items()
}
)
Expand Down
31 changes: 24 additions & 7 deletions tests/expr_and_series/rolling_std_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import annotations

from math import sqrt
from typing import Any

import numpy as np
import pytest

import narwhals.stable.v1 as nw
Expand All @@ -17,32 +17,49 @@
{
"name": "x1",
"kwargs": {"window_size": 3},
"expected": np.sqrt([float("nan"), float("nan"), 1 / 3, 1, 4 / 3, 7 / 3, 3]),
"expected": [
sqrt(x) if x is not None else x
for x in [None, None, 1 / 3, 1, 4 / 3, 7 / 3, 3]
],
},
{
"name": "x2",
"kwargs": {"window_size": 3, "min_periods": 1},
"expected": np.sqrt([float("nan"), 0.5, 1 / 3, 1.0, 4 / 3, 7 / 3, 3]),
"expected": [
sqrt(x) if x is not None else x
for x in [None, 0.5, 1 / 3, 1.0, 4 / 3, 7 / 3, 3]
],
},
{
"name": "x3",
"kwargs": {"window_size": 2, "min_periods": 1},
"expected": np.sqrt([float("nan"), 0.5, 0.5, 2.0, 2.0, 4.5, 4.5]),
"expected": [
sqrt(x) if x is not None else x for x in [None, 0.5, 0.5, 2.0, 2.0, 4.5, 4.5]
],
},
{
"name": "x4",
"kwargs": {"window_size": 5, "min_periods": 1, "center": True},
"expected": np.sqrt([1 / 3, 11 / 12, 4 / 5, 17 / 10, 2.0, 2.25, 3]),
"expected": [
sqrt(x) if x is not None else x
for x in [1 / 3, 11 / 12, 4 / 5, 17 / 10, 2.0, 2.25, 3]
],
},
{
"name": "x5",
"kwargs": {"window_size": 4, "min_periods": 1, "center": True},
"expected": np.sqrt([0.5, 1 / 3, 11 / 12, 11 / 12, 2.25, 2.25, 3]),
"expected": [
sqrt(x) if x is not None else x
for x in [0.5, 1 / 3, 11 / 12, 11 / 12, 2.25, 2.25, 3]
],
},
{
"name": "x6",
"kwargs": {"window_size": 3, "ddof": 2},
"expected": np.sqrt([float("nan"), float("nan"), 2 / 3, 2.0, 8 / 3, 14 / 3, 6.0]),
"expected": [
sqrt(x) if x is not None else x
for x in [None, None, 2 / 3, 2.0, 8 / 3, 14 / 3, 6.0]
],
},
)

Expand Down
12 changes: 6 additions & 6 deletions tests/expr_and_series/rolling_sum_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

data = {"a": [None, 1, 2, None, 4, 6, 11]}

kwargs_and_expected = {
"x1": {"kwargs": {"window_size": 3}, "expected": [float("nan")] * 6 + [21]},
kwargs_and_expected: dict[str, dict[str, Any]] = {
"x1": {"kwargs": {"window_size": 3}, "expected": [None] * 6 + [21]},
"x2": {
"kwargs": {"window_size": 3, "min_periods": 1},
"expected": [float("nan"), 1.0, 3.0, 3.0, 6.0, 10.0, 21.0],
"expected": [None, 1.0, 3.0, 3.0, 6.0, 10.0, 21.0],
},
"x3": {
"kwargs": {"window_size": 2, "min_periods": 1},
"expected": [float("nan"), 1.0, 3.0, 2.0, 4.0, 10.0, 17.0],
"expected": [None, 1.0, 3.0, 2.0, 4.0, 10.0, 17.0],
},
"x4": {
"kwargs": {"window_size": 5, "min_periods": 1, "center": True},
Expand Down Expand Up @@ -54,7 +54,7 @@ def test_rolling_sum_expr(
df = nw.from_native(constructor(data))
result = df.select(
**{
name: nw.col("a").rolling_sum(**values["kwargs"]) # type: ignore[arg-type]
name: nw.col("a").rolling_sum(**values["kwargs"])
for name, values in kwargs_and_expected.items()
}
)
Expand All @@ -71,7 +71,7 @@ def test_rolling_sum_series(constructor_eager: ConstructorEager) -> None:

result = df.select(
**{
name: df["a"].rolling_sum(**values["kwargs"]) # type: ignore[arg-type]
name: df["a"].rolling_sum(**values["kwargs"])
for name, values in kwargs_and_expected.items()
}
)
Expand Down
8 changes: 4 additions & 4 deletions tests/expr_and_series/rolling_var_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
{
"name": "x1",
"kwargs": {"window_size": 3},
"expected": [float("nan"), float("nan"), 1 / 3, 1, 4 / 3, 7 / 3, 3],
"expected": [None, None, 1 / 3, 1, 4 / 3, 7 / 3, 3],
},
{
"name": "x2",
"kwargs": {"window_size": 3, "min_periods": 1},
"expected": [float("nan"), 0.5, 1 / 3, 1.0, 4 / 3, 7 / 3, 3],
"expected": [None, 0.5, 1 / 3, 1.0, 4 / 3, 7 / 3, 3],
},
{
"name": "x3",
"kwargs": {"window_size": 2, "min_periods": 1},
"expected": [float("nan"), 0.5, 0.5, 2.0, 2.0, 4.5, 4.5],
"expected": [None, 0.5, 0.5, 2.0, 2.0, 4.5, 4.5],
},
{
"name": "x4",
Expand All @@ -48,7 +48,7 @@
{
"name": "x6",
"kwargs": {"window_size": 3, "ddof": 2},
"expected": [float("nan"), float("nan"), 2 / 3, 2.0, 8 / 3, 14 / 3, 6.0],
"expected": [None, None, 2 / 3, 2.0, 8 / 3, 14 / 3, 6.0],
},
)

Expand Down
4 changes: 2 additions & 2 deletions tests/expr_and_series/skew_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
("data", "expected"),
[
([], None),
([1], float("nan")),
([1], None),
([1, 2], 0.0),
([0.0, 0.0, 0.0], float("nan")),
([0.0, 0.0, 0.0], None),
([1, 2, 3, 2, 1], 0.343622),
],
)
Expand Down
20 changes: 10 additions & 10 deletions tests/expr_and_series/unary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_unary_two_elements(constructor: Constructor) -> None:
"b_nunique": [2],
"b_skew": [0.0],
"c_nunique": [2],
"c_skew": [float("nan")],
"c_skew": [None],
}
assert_equal_data(result, expected)

Expand All @@ -115,13 +115,13 @@ def test_unary_two_elements_series(constructor_eager: ConstructorEager) -> None:
"b_nunique": [2],
"b_skew": [0.0],
"c_nunique": [2],
"c_skew": [float("nan")],
"c_skew": [None],
}
assert_equal_data(result, expected)


def test_unary_one_element(constructor: Constructor) -> None:
data = {"a": [1], "b": [2], "c": [float("nan")]}
data = {"a": [1], "b": [2], "c": [None]}
# Dask runs into a divide by zero RuntimeWarning for 1 element skew.
context = (
pytest.warns(RuntimeWarning, match="invalid value encountered in scalar divide")
Expand All @@ -139,17 +139,17 @@ def test_unary_one_element(constructor: Constructor) -> None:
)
expected = {
"a_nunique": [1],
"a_skew": [float("nan")],
"a_skew": [None],
"b_nunique": [1],
"b_skew": [float("nan")],
"b_skew": [None],
"c_nunique": [1],
"c_skew": [float("nan")],
"c_skew": [None],
}
assert_equal_data(result, expected)


def test_unary_one_element_series(constructor_eager: ConstructorEager) -> None:
data = {"a": [1], "b": [2], "c": [float("nan")]}
data = {"a": [1], "b": [2], "c": [None]}
df = nw.from_native(constructor_eager(data))
result = {
"a_nunique": [df["a"].n_unique()],
Expand All @@ -161,10 +161,10 @@ def test_unary_one_element_series(constructor_eager: ConstructorEager) -> None:
}
expected = {
"a_nunique": [1],
"a_skew": [float("nan")],
"a_skew": [None],
"b_nunique": [1],
"b_skew": [float("nan")],
"b_skew": [None],
"c_nunique": [1],
"c_skew": [float("nan")],
"c_skew": [None],
}
assert_equal_data(result, expected)
11 changes: 5 additions & 6 deletions tests/expr_and_series/when_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_when(constructor: Constructor) -> None:
df = nw.from_native(constructor(data))
result = df.select(nw.when(nw.col("a") == 1).then(value=3).alias("a_when"))
expected = {
"a_when": [3, np.nan, np.nan],
"a_when": [3, None, None],
}
assert_equal_data(result, expected)

Expand All @@ -41,7 +41,7 @@ def test_multiple_conditions(constructor: Constructor) -> None:
nw.when(nw.col("a") < 3, nw.col("c") < 5.0).then(3).alias("a_when")
)
expected = {
"a_when": [3, np.nan, np.nan],
"a_when": [3, None, None],
}
assert_equal_data(result, expected)

Expand All @@ -65,7 +65,7 @@ def test_value_numpy_array(
nw.when(nw.col("a") == 1).then(np.asanyarray([3, 4, 5])).alias("a_when")
)
expected = {
"a_when": [3, np.nan, np.nan],
"a_when": [3, None, None],
}
assert_equal_data(result, expected)

Expand All @@ -77,7 +77,7 @@ def test_value_series(constructor_eager: ConstructorEager) -> None:
assert isinstance(s, nw.Series)
result = df.select(nw.when(nw.col("a") == 1).then(s).alias("a_when"))
expected = {
"a_when": [3, np.nan, np.nan],
"a_when": [3, None, None],
}
assert_equal_data(result, expected)

Expand All @@ -86,7 +86,7 @@ def test_value_expression(constructor: Constructor) -> None:
df = nw.from_native(constructor(data))
result = df.select(nw.when(nw.col("a") == 1).then(nw.col("a") + 9).alias("a_when"))
expected = {
"a_when": [10, np.nan, np.nan],
"a_when": [10, None, None],
}
assert_equal_data(result, expected)

Expand All @@ -98,7 +98,6 @@ def test_otherwise_numpy_array(
request.applymarker(pytest.mark.xfail)

df = nw.from_native(constructor(data))
import numpy as np

result = df.select(
nw.when(nw.col("a") == 1).then(-1).otherwise(np.array([0, 9, 10])).alias("a_when")
Expand Down
4 changes: 2 additions & 2 deletions tests/frame/drop_nulls_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def test_drop_nulls(constructor: Constructor) -> None:
@pytest.mark.parametrize(
("subset", "expected"),
[
("a", {"a": [1, 2.0, 4.0], "b": [float("nan"), 3.0, 5.0]}),
(["a"], {"a": [1, 2.0, 4.0], "b": [float("nan"), 3.0, 5.0]}),
("a", {"a": [1, 2.0, 4.0], "b": [None, 3.0, 5.0]}),
(["a"], {"a": [1, 2.0, 4.0], "b": [None, 3.0, 5.0]}),
(["a", "b"], {"a": [2.0, 4.0], "b": [3.0, 5.0]}),
],
)
Expand Down
Loading

0 comments on commit 50037a5

Please sign in to comment.