Skip to content

Commit

Permalink
chore: replace constructor_series with constructor (#649)
Browse files Browse the repository at this point in the history
* ref

* chore: replace constructor_series with constructor
  • Loading branch information
MarcoGorelli authored Jul 27, 2024
1 parent a9c34dd commit d3cf744
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 70 deletions.
13 changes: 1 addition & 12 deletions narwhals/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from __future__ import annotations

import sys
import warnings
from typing import TYPE_CHECKING
from typing import Any

Expand Down Expand Up @@ -73,17 +72,7 @@ def get_dask() -> Any:

def get_dask_dataframe() -> Any:
"""Get dask.dataframe module (if already imported - else return None)."""
if "dask" in sys.modules:
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="\nDask dataframe query planning.*",
category=FutureWarning,
)
import dask.dataframe as dd

return dd
return None # pragma: no cover
return sys.modules.get("dask.dataframe", None)


def get_dask_expr() -> Any:
Expand Down
4 changes: 2 additions & 2 deletions tests/expr_and_series/is_in_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def test_expr_is_in(constructor: Any) -> None:
compare_dicts(result, expected)


def test_ser_is_in(constructor_series: Any) -> None:
ser = nw.from_native(constructor_series(series), series_only=True)
def test_ser_is_in(constructor: Any) -> None:
ser = nw.from_native(constructor({"a": series}), eager_only=True)["a"]
result = ser.is_in([4, 5]).to_list()
assert not result[0]
assert result[1]
Expand Down
6 changes: 3 additions & 3 deletions tests/expr_and_series/sort_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def test_sort_expr(
],
)
def test_sort_series(
constructor_series: Any, descending: Any, nulls_last: Any, expected: Any
constructor: Any, descending: Any, nulls_last: Any, expected: Any
) -> None:
series = nw.from_native(constructor_series(data["b"]), series_only=True)
series = nw.from_native(constructor(data), eager_only=True)["b"]
result = series.sort(descending=descending, nulls_last=nulls_last)
assert result == nw.from_native(constructor_series(expected), series_only=True)
assert result == nw.from_native(constructor({"a": expected}), eager_only=True)["a"]
8 changes: 4 additions & 4 deletions tests/series_only/arithmetic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ def test_arithmetic(
attr: str,
rhs: Any,
expected: list[Any],
constructor_series: Any,
constructor: Any,
) -> None:
if "pandas_series_pyarrow" in str(constructor_series) and attr == "__mod__":
if "pandas_pyarrow" in str(constructor) and attr == "__mod__":
request.applymarker(pytest.mark.xfail)

if "pyarrow_series" in str(constructor_series) and attr == "__mod__":
if "table" in str(constructor) and attr == "__mod__":
request.applymarker(pytest.mark.xfail)

s = nw.from_native(constructor_series(data), series_only=True)
s = nw.from_native(constructor({"a": data}), eager_only=True)["a"]
result = getattr(s, attr)(rhs)
assert result.to_numpy().tolist() == expected

Expand Down
4 changes: 2 additions & 2 deletions tests/series_only/filter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import narwhals as nw


def test_filter(constructor_series: Any) -> None:
def test_filter(constructor: Any) -> None:
data = [1, 3, 2]
series = nw.from_native(constructor_series(data), series_only=True)
series = nw.from_native(constructor({"a": data}), eager_only=True)["a"]
result = series.filter(series > 1)
expected = np.array([3, 2])
assert (result.to_numpy() == expected).all()
4 changes: 2 additions & 2 deletions tests/series_only/is_between_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
("none", [False, True, True, False]),
],
)
def test_is_between(constructor_series: Any, closed: str, expected: list[bool]) -> None:
ser = nw.from_native(constructor_series(data), series_only=True)
def test_is_between(constructor: Any, closed: str, expected: list[bool]) -> None:
ser = nw.from_native(constructor({"a": data}), eager_only=True)["a"]
result = ser.is_between(1, 5, closed=closed)
compare_dicts({"a": result}, {"a": expected})
8 changes: 4 additions & 4 deletions tests/series_only/is_sorted_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
[(data, False, False), (data_sorted, False, True), (data_sorted, True, False)],
)
def test_is_sorted(
constructor_series: Any,
constructor: Any,
input_data: str,
descending: bool, # noqa: FBT001
expected: bool, # noqa: FBT001
) -> None:
series = nw.from_native(constructor_series(input_data), series_only=True)
series = nw.from_native(constructor({"a": input_data}), eager_only=True)["a"]
result = series.is_sorted(descending=descending)
compare_dicts({"a": [result]}, {"a": [expected]})


def test_is_sorted_invalid(constructor_series: Any) -> None:
series = nw.from_native(constructor_series(data_sorted), series_only=True)
def test_is_sorted_invalid(constructor: Any) -> None:
series = nw.from_native(constructor({"a": data_sorted}), eager_only=True)["a"]

with pytest.raises(TypeError):
series.is_sorted(descending="invalid_type") # type: ignore[arg-type]
4 changes: 2 additions & 2 deletions tests/series_only/null_count_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import narwhals as nw


def test_null_count(constructor_series: Any) -> None:
def test_null_count(constructor: Any) -> None:
data = [1, 2, None]
series = nw.from_native(constructor_series(data), series_only=True)
series = nw.from_native(constructor({"a": data}), eager_only=True)["a"]
result = series.null_count()
assert result == 1
12 changes: 5 additions & 7 deletions tests/series_only/operators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
],
)
def test_comparand_operators(
constructor_series: Any, operator: str, expected: list[bool]
constructor: Any, operator: str, expected: list[bool]
) -> None:
data = [0, 1, 2]
s = nw.from_native(constructor_series(data), series_only=True)
s = nw.from_native(constructor({"a": data}), eager_only=True)["a"]
result = getattr(s, operator)(1)
assert result.to_list() == expected

Expand All @@ -34,12 +34,10 @@ def test_comparand_operators(
("__or__", [True, True, True, False]),
],
)
def test_logic_operators(
constructor_series: Any, operator: str, expected: list[bool]
) -> None:
def test_logic_operators(constructor: Any, operator: str, expected: list[bool]) -> None:
data = [True, True, False, False]
other_data = [True, False, True, False]
series = nw.from_native(constructor_series(data), series_only=True)
other = nw.from_native(constructor_series(other_data), series_only=True)
series = nw.from_native(constructor({"a": data}), eager_only=True)["a"]
other = nw.from_native(constructor({"a": other_data}), eager_only=True)["a"]
result = getattr(series, operator)(other)
assert result.to_list() == expected
39 changes: 18 additions & 21 deletions tests/series_only/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
data_sorted = [7.0, 8, 9]


def test_len(constructor_series: Any) -> None:
series = nw.from_native(constructor_series(data), series_only=True)
def test_len(constructor: Any) -> None:
series = nw.from_native(constructor({"a": data}), eager_only=True)["a"]

result = len(series)
assert result == 3
Expand All @@ -28,8 +28,8 @@ def test_len(constructor_series: Any) -> None:
assert result == 3


def test_is_in(constructor_series: Any) -> None:
series = nw.from_native(constructor_series(data), series_only=True)
def test_is_in(constructor: Any) -> None:
series = nw.from_native(constructor({"a": data}), eager_only=True)["a"]

result = series.is_in([1, 2]).to_list()
assert result[0]
Expand All @@ -48,18 +48,18 @@ def test_is_in_other(constructor: Any) -> None:
nw.from_native(df_raw).with_columns(contains=nw.col("a").is_in("sets"))


def test_dtype(constructor_series: Any) -> None:
series = nw.from_native(constructor_series(data), series_only=True)
def test_dtype(constructor: Any) -> None:
series = nw.from_native(constructor({"a": data}), eager_only=True)["a"]
result = series.dtype
assert result == nw.Int64
assert result.is_numeric()


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

s = nw.from_native(constructor_series(data), series_only=True)
s = nw.from_native(constructor({"a": data}), eager_only=True)["a"]
assert s.mean() == 2.0
assert s.std() == 1.0
assert s.min() == 1
Expand Down Expand Up @@ -90,14 +90,11 @@ def test_boolean_reductions(request: Any, constructor: Any) -> None:
@pytest.mark.skipif(
parse_version(pd.__version__) < parse_version("2.0.0"), reason="too old for pyarrow"
)
def test_convert(request: Any, constructor_series: Any) -> None:
if any(
cname in str(constructor_series)
for cname in ("pandas_series_nullable", "pandas_series_pyarrow")
):
def test_convert(request: Any, constructor: Any) -> None:
if any(cname in str(constructor) for cname in ("pandas_nullable", "pandas_pyarrow")):
request.applymarker(pytest.mark.xfail)

series = nw.from_native(constructor_series(data), series_only=True).alias("a")
series = nw.from_native(constructor({"a": data}), eager_only=True)["a"].alias("a")

result = series.to_numpy()
assert_array_equal(result, np.array([1, 3, 2]))
Expand All @@ -114,10 +111,10 @@ def test_to_numpy() -> None:
assert nw_series.shape == (3,)


def test_zip_with(constructor_series: Any) -> None:
series1 = nw.from_native(constructor_series(data), series_only=True)
series2 = nw.from_native(constructor_series(data_dups), series_only=True)
mask = nw.from_native(constructor_series([True, False, True]), series_only=True)
def test_zip_with(constructor: Any) -> None:
series1 = nw.from_native(constructor({"a": data}), eager_only=True)["a"]
series2 = nw.from_native(constructor({"a": data_dups}), eager_only=True)["a"]
mask = nw.from_native(constructor({"a": [True, False, True]}), eager_only=True)["a"]

result = series1.zip_with(mask, series2)
expected = [1, 4, 2]
Expand All @@ -137,8 +134,8 @@ def test_cast_string() -> None:


@pytest.mark.parametrize(("index", "expected"), [(0, 1), (1, 3)])
def test_item(constructor_series: Any, index: int, expected: int) -> None:
series = nw.from_native(constructor_series(data), series_only=True)
def test_item(constructor: Any, index: int, expected: int) -> None:
series = nw.from_native(constructor({"a": data}), eager_only=True)["a"]
result = series.item(index)
compare_dicts({"a": [result]}, {"a": [expected]})
compare_dicts({"a": [series.head(1).item()]}, {"a": [1]})
Expand Down
8 changes: 4 additions & 4 deletions tests/series_only/to_dummy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@


@pytest.mark.parametrize("sep", ["_", "-"])
def test_to_dummies(constructor_series: Any, sep: str) -> None:
s = nw.from_native(constructor_series(data), series_only=True).alias("a")
def test_to_dummies(constructor: Any, sep: str) -> None:
s = nw.from_native(constructor({"a": data}), eager_only=True)["a"].alias("a")
result = s.to_dummies(separator=sep)
expected = {f"a{sep}1": [1, 0, 0], f"a{sep}2": [0, 1, 0], f"a{sep}3": [0, 0, 1]}

compare_dicts(result, expected)


@pytest.mark.parametrize("sep", ["_", "-"])
def test_to_dummies_drop_first(constructor_series: Any, sep: str) -> None:
s = nw.from_native(constructor_series(data), series_only=True).alias("a")
def test_to_dummies_drop_first(constructor: Any, sep: str) -> None:
s = nw.from_native(constructor({"a": data}), eager_only=True)["a"].alias("a")
result = s.to_dummies(drop_first=True, separator=sep)
expected = {f"a{sep}2": [0, 1, 0], f"a{sep}3": [0, 0, 1]}

Expand Down
8 changes: 6 additions & 2 deletions tests/series_only/to_frame_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
data = [1, 2, 3]


def test_to_frame(constructor_series: Any) -> None:
df = nw.from_native(constructor_series(data), series_only=True).alias("").to_frame()
def test_to_frame(constructor: Any) -> None:
df = (
nw.from_native(constructor({"a": data}), eager_only=True)["a"]
.alias("")
.to_frame()
)
compare_dicts(df, {"": [1, 2, 3]})
4 changes: 2 additions & 2 deletions tests/series_only/to_list_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
data = [1, 2, 3]


def test_to_list(constructor_series: Any) -> None:
s = nw.from_native(constructor_series(data), series_only=True)
def test_to_list(constructor: Any) -> None:
s = nw.from_native(constructor({"a": data}), eager_only=True)["a"]
assert s.to_list() == [1, 2, 3]
6 changes: 3 additions & 3 deletions tests/series_only/value_counts_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
@pytest.mark.parametrize("normalize", [True, False])
@pytest.mark.parametrize("name", [None, "count_name"])
def test_value_counts(
request: Any, constructor_series: Any, normalize: Any, name: str | None
request: Any, constructor: Any, normalize: Any, name: str | None
) -> None:
if "pandas_series_nullable_constructor" in str(constructor_series) and parse_version(
if "pandas_nullable_constructor" in str(constructor) and parse_version(
pd.__version__
) < (2, 2):
# bug in old pandas
Expand All @@ -32,7 +32,7 @@ def test_value_counts(
expected_name = name or ("proportion" if normalize else "count")
expected = {"a": expected_index, expected_name: expected_count}

series = nw.from_native(constructor_series(data), series_only=True).alias("a")
series = nw.from_native(constructor({"a": data}), eager_only=True)["a"].alias("a")

sorted_result = series.value_counts(sort=True, name=name, normalize=normalize)
compare_dicts(sorted_result, expected)
Expand Down

0 comments on commit d3cf744

Please sign in to comment.