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

fix: _create_compliant_series #508

Merged
merged 1 commit into from
Jul 13, 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
10 changes: 8 additions & 2 deletions narwhals/_arrow/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,15 @@ def _create_series_from_scalar(self, value: Any, series: ArrowSeries) -> ArrowSe
backend_version=self._backend_version,
)

def _create_native_series(self, value: Any) -> Any: # pragma: no cover (todo!)
def _create_compliant_series(self, value: Any) -> ArrowSeries:
from narwhals._arrow.series import ArrowSeries

pa = get_pyarrow()
return pa.chunked_array([value])
return ArrowSeries(
native_series=pa.chunked_array([value]),
name="",
backend_version=self._backend_version,
)

# --- not in spec ---
def __init__(self, *, backend_version: tuple[int, ...]) -> None:
Expand Down
4 changes: 2 additions & 2 deletions narwhals/_expression_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ def parse_into_expr(
if isinstance(into_expr, str):
return namespace.col(into_expr)
if (np := get_numpy()) is not None and isinstance(into_expr, np.ndarray):
series = namespace._create_native_series(into_expr)
return namespace._create_expr_from_series(series)
series = namespace._create_compliant_series(into_expr)
return namespace._create_expr_from_series(series) # type: ignore[arg-type]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mypy complains 😭

msg = f"Expected IntoExpr, got {type(into_expr)}" # pragma: no cover
raise AssertionError(msg)

Expand Down
2 changes: 1 addition & 1 deletion narwhals/_pandas_like/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def _create_expr_from_series(self, series: PandasLikeSeries) -> PandasLikeExpr:
backend_version=self._backend_version,
)

def _create_native_series(self, value: Any) -> Any:
def _create_compliant_series(self, value: Any) -> PandasLikeSeries:
return create_native_series(
value,
implementation=self._implementation,
Expand Down
11 changes: 8 additions & 3 deletions tests/frame/with_columns_sequence_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
}


def test_with_columns(constructor: Any) -> None:
result = nw.from_native(constructor(data)).with_columns(d=np.array([4, 5]))
expected = {"a": ["foo", "bars"], "ab": ["foo", "bars"], "d": [4, 5]}
def test_with_columns(constructor_with_pyarrow: Any) -> None:
result = (
nw.from_native(constructor_with_pyarrow(data))
.with_columns(d=np.array([4, 5]))
.with_columns(e=nw.col("d") + 1)
.select("d", "e")
)
expected = {"d": [4, 5], "e": [5, 6]}
compare_dicts(result, expected)
Loading