Skip to content
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
15 changes: 9 additions & 6 deletions narwhals/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
is_list_of,
is_sequence_like,
is_slice_none,
qualified_type_name,
supports_arrow_c_stream,
zip_strict,
)
Expand Down Expand Up @@ -139,6 +140,12 @@ def _flatten_and_extract(
def _extract_compliant(self, arg: Any) -> Any:
raise NotImplementedError

def _extract_compliant_frame(self, other: Self | Any, /) -> Any:
if isinstance(other, type(self)):
return other._compliant_frame
msg = f"Expected `other` to be a {qualified_type_name(self)!r}, got: {qualified_type_name(other)!r}"
raise TypeError(msg)

def _check_columns_exist(self, subset: Sequence[str]) -> ColumnNotFoundError | None:
return check_columns_exist(subset, available=self.columns)

Expand Down Expand Up @@ -269,7 +276,7 @@ def join(
left_on = [left_on] if isinstance(left_on, str) else left_on
right_on = [right_on] if isinstance(right_on, str) else right_on
compliant = self._compliant_frame
other = self._extract_compliant(other)
other = self._extract_compliant_frame(other)

if how not in _supported_joins:
msg = f"Only the following join strategies are supported: {_supported_joins}; found '{how}'."
Expand Down Expand Up @@ -357,7 +364,7 @@ def join_asof(

return self._with_compliant(
self._compliant_frame.join_asof(
self._extract_compliant(other),
self._extract_compliant_frame(other),
left_on=left_on,
right_on=right_on,
by_left=by_left,
Expand Down Expand Up @@ -448,8 +455,6 @@ def _extract_compliant(self, arg: Any) -> Any:
from narwhals.series import Series

plx: EagerNamespaceAny = self.__narwhals_namespace__()
if isinstance(arg, BaseFrame):
return arg._compliant_frame
if isinstance(arg, Series):
return arg._compliant_series._to_expr()
if isinstance(arg, Expr):
Expand Down Expand Up @@ -2296,8 +2301,6 @@ def _extract_compliant(self, arg: Any) -> Any:
from narwhals.expr import Expr
from narwhals.series import Series

if isinstance(arg, BaseFrame):
return arg._compliant_frame
if isinstance(arg, Series): # pragma: no cover
msg = "Binary operations between Series and LazyFrame are not supported."
raise TypeError(msg)
Expand Down
3 changes: 0 additions & 3 deletions narwhals/stable/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,9 @@ def _dataframe(self) -> type[DataFrame[Any]]:
def _extract_compliant(self, arg: Any) -> Any:
# After v1, we raise when passing order-dependent or length-changing
# expressions to LazyFrame
from narwhals.dataframe import BaseFrame
from narwhals.expr import Expr
from narwhals.series import Series

if isinstance(arg, BaseFrame):
return arg._compliant_frame
if isinstance(arg, Series): # pragma: no cover
msg = "Mixing Series with LazyFrame is not supported."
raise TypeError(msg)
Expand Down
20 changes: 20 additions & 0 deletions tests/frame/join_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,3 +798,23 @@ def test_join_duplicate_column_names(
else:
with pytest.raises(exception):
df.join(df, on=["a"]).join(df, on=["a"])


def test_join_same_laziness(constructor: Constructor) -> None:
pytest.importorskip("polars")
import polars as pl

data_left = {"id": [1, 2, 3], "age": [25, 30, 35]}
data_right = {"id": [2, 3, 4], "active": [False, True, True]}
frame = nw.from_native(constructor(data_left))
df_pl = pl.DataFrame(data_right)
frame_pl: pl.DataFrame | pl.LazyFrame
if isinstance(frame, nw.DataFrame):
msg = r"Expected.+\.DataFrame.+got.+\.LazyFrame"
frame_pl = df_pl.lazy()
else:
msg = r"Expected.+\.LazyFrame.+got.+\.DataFrame"
frame_pl = df_pl
other = nw.from_native(frame_pl)
with pytest.raises(TypeError, match=msg):
frame.join(other, on="id") # type: ignore[arg-type]
Loading