Skip to content

Commit

Permalink
Merge pull request #55 from DeaMariaLeon/message
Browse files Browse the repository at this point in the history
Adding error message when different libraries are used
  • Loading branch information
MarcoGorelli authored May 1, 2024
2 parents 063b0b5 + 90f494b commit d617ebb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions narwhals/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from narwhals.translate import get_modin
from narwhals.translate import get_pandas
from narwhals.translate import get_polars
from narwhals.utils import validate_same_library

if TYPE_CHECKING:
import numpy as np
Expand Down Expand Up @@ -133,6 +134,7 @@ def join(
) -> Self:
if how != "inner":
raise NotImplementedError("Only inner joins are supported for now")
validate_same_library([self, other])
return self._from_dataframe(
self._dataframe.join(
self._extract_native(other),
Expand Down
3 changes: 3 additions & 0 deletions narwhals/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from typing import Iterable
from typing import Literal

from narwhals.utils import validate_same_library

if TYPE_CHECKING:
from narwhals.dataframe import DataFrame
from narwhals.dataframe import LazyFrame
Expand All @@ -21,6 +23,7 @@ def concat(
if not items:
raise ValueError("No items to concatenate")
items = list(items)
validate_same_library(items)
first_item = items[0]
plx = first_item.__narwhals_namespace__()
return first_item.__class__(
Expand Down
10 changes: 10 additions & 0 deletions narwhals/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,13 @@ def parse_version(version: Sequence[str | int]) -> tuple[int, ...]:

def isinstance_or_issubclass(obj: Any, cls: Any) -> bool:
return isinstance(obj, cls) or issubclass(obj, cls)


def validate_same_library(items: Iterable[Any]) -> None:
if all(item._is_polars for item in items):
return
if all(hasattr(item._dataframe, "_implementation") for item in items) and (
len({item._dataframe._implementation for item in items}) == 1
):
return
raise NotImplementedError("Cross-library comparisons aren't supported")
21 changes: 21 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,3 +630,24 @@ def test_reindex(df_raw: Any) -> None:
compare_dicts(result, expected)
with pytest.raises(ValueError, match="Multi-output expressions are not supported"):
nw.to_native(df.with_columns(nw.all() + nw.all()))


@pytest.mark.parametrize(
("df_raw", "df_raw_right"),
[(df_pandas, df_polars), (df_polars, df_pandas)],
)
def test_library(df_raw: Any, df_raw_right: Any) -> None:
df_left = nw.LazyFrame(df_raw)
df_right = nw.LazyFrame(df_raw_right)
with pytest.raises(
NotImplementedError, match="Cross-library comparisons aren't supported"
):
nw.concat([df_left, df_right], how="horizontal")
with pytest.raises(
NotImplementedError, match="Cross-library comparisons aren't supported"
):
nw.concat([df_left, df_right], how="vertical")
with pytest.raises(
NotImplementedError, match="Cross-library comparisons aren't supported"
):
df_left.join(df_right, left_on=["a"], right_on=["a"], how="inner")

0 comments on commit d617ebb

Please sign in to comment.